Geocoder.getFromLocationName() throws “Service not available” exception on Android 4 with Google Maps V2

ⅰ亾dé卋堺 提交于 2019-12-04 02:13:28

问题


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?


回答1:


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.




回答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));


来源:https://stackoverflow.com/questions/16655549/geocoder-getfromlocationname-throws-service-not-available-exception-on-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!