The method Geocoder.getFromLocationName() throws the exception Service not available on Android 4.1, even if GooglePlayServicesUtil.isGooglePlayServicesAvailable() returns SUCCESS and Geocoder.isPresent() returns true.
Is there any official example for geocoding in the new Google Maps v2 API?
Asked
Active
Viewed 3,850 times
1
-
I found a workaround for this: reboot the device. But it's not a solution... – Umberto May 20 '13 at 18:06
-
I'm still finding a way to solve this bug. Any ideas? – Umberto Jul 04 '13 at 13:19
-
Any ideas? Seems the Geocoder doesn't work on Android 4, with Google Maps V2 – Umberto Jul 14 '13 at 16:46
2 Answers
3
Geocoder is not related to Google Maps Android API v2.
You may want to use Google Geocoding API directly instead of Geocoder, which gives you limited amount of data and might be affected by device or Android version specific problems.
MaciejGórski
- 22,050
- 7
- 68
- 93
-
I didn't know that Geocoder is not related to the v2 API. Anyway, have I to use [this](https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests) method? – Umberto May 20 '13 at 19:24
-
I see there are problems even with Google Geocoding API so I have to implement a solution that includes Geocoder class. Any ideas? – Umberto Jul 14 '13 at 16:47
2
try this .....
public JSONObject getLocationFormGoogle(String placesName) {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
public LatLng getLatLng(JSONObject jsonObject) {
Double lon = new Double(0);
Double lat = new Double(0);
try {
lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new LatLng(lat,lon);
}
LatLng Source =getLatLng(getLocationFormGoogle(placesName));
Anil M H
- 3,282
- 5
- 33
- 50
-
Thanks a lot for this, it worked perfectly. just wanted to now that if the name has spaces it will crash. so it would be a nice idea to edit the code to replace the white space with a `%20` in the `placesName` before doing the request to avoid the crash – Y2theZ Feb 11 '15 at 03:32