Skip to content Skip to sidebar Skip to footer

Google Maps Reverse Geocoding Via HTTPS In Android

I am making google map app. Since Geocoder class returns empty(out of bounds at [0]) i am currently trying the HTTPS Google maps reverse GeoCoding. This is my onClick call: publi

Solution 1:

Since the AsyncTask is a subclass of the Activity, you just need to assign the LatLng to a member variable of the Activity so that it's accessible in the AsyncTask.

Then, add the Marker in onPostExecute().

First, the change to the Activity code. Make a LatLng member variable in addition to the JSONArray:

public class MyMapActivity extends AppCompatActivity implements OnMapReadyCallback
{

    JSONArray js;
    LatLng currLatLng;
    //...............

Then, modify the onMapClick to assign the LatLng to the instance variable:

@Override
public void onMapClick(LatLng latLng) {
    JSONArray js=null;
    try {

         //assign to member variable:
         currLatLng = latLng;

         //don't use the return value:
         getAddress(latLng);

         //remove this:
         //mMap.addMarker(new MarkerOptions().position(latLng).title(js.getString(0)).draggable(true));//NullPointer
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException p) {
        //todo

    }

Then, use onPostExecute to place the marker after you have the response. The other main issue is that the JSON response is a JSONObject that contains a JSONArray inside it. Fixed in the code below:

public class Async extends AsyncTask<URL, Void, JSONArray> {
    public JSONArray jsonArray;

    @Override
    protected JSONArray doInBackground(URL... params) {
        BufferedReader reader;
        InputStream is;
        try {

            StringBuilder responseBuilder = new StringBuilder();
            HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            for (String line; (line = reader.readLine()) != null; ) {
                responseBuilder.append(line).append("\n");
            }
            JSONObject jObj= new JSONObject(responseBuilder.toString());
            jsonArray = jObj.getJSONArray("results");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonArray;
    }


    @Override
    protected void onPostExecute(JSONArray jsonArrayResponse) {
        js = jsonArrayResponse;
        try {
            if (js != null) {
              JSONObject jsFirstAddress = js.getJSONObject(0);
              mMap.addMarker(new MarkerOptions().position(currLatLng).title(jsFirstAddress.getString("formatted_address")).draggable(true));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Note that you don't need to return anything from getAddress(), so just make the return type void and let the AsyncTask do the rest once it's executed:

  public void getAddress(LatLng latLng) throws IOException {
    URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
    Async async =new Async();
    async.execute(url);
    //JSONArray response = async.jsonArray;
    //return response;
  }

Post a Comment for "Google Maps Reverse Geocoding Via HTTPS In Android"