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:
publicclassMyMapActivityextendsAppCompatActivityimplementsOnMapReadyCallback
{
JSONArray js;
LatLng currLatLng;
//...............
Then, modify the onMapClick to assign the LatLng to the instance variable:
@OverridepublicvoidonMapClick(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:
publicclassAsyncextendsAsyncTask<URL, Void, JSONArray> {
publicJSONArray jsonArray;
@OverrideprotectedJSONArraydoInBackground(URL... params) {
BufferedReader reader;
InputStream is;
try {
StringBuilder responseBuilder = newStringBuilder();
HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
reader = newBufferedReader(newInputStreamReader(is));
for (String line; (line = reader.readLine()) != null; ) {
responseBuilder.append(line).append("\n");
}
JSONObject jObj= newJSONObject(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;
}
@OverrideprotectedvoidonPostExecute(JSONArray jsonArrayResponse) {
js = jsonArrayResponse;
try {
if (js != null) {
JSONObject jsFirstAddress = js.getJSONObject(0);
mMap.addMarker(newMarkerOptions().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:
publicvoidgetAddress(LatLng latLng)throws IOException {
URLurl=newURL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
Asyncasync=newAsync();
async.execute(url);
//JSONArray response = async.jsonArray;//return response;
}
Post a Comment for "Google Maps Reverse Geocoding Via Https In Android"