1

该方法在 Android 4.1 上Geocoder.getFromLocationName()抛出异常Service not available,即使GooglePlayServicesUtil.isGooglePlayServicesAvailable()返回SUCCESSGeocoder.isPresent()返回true
在新的 Google Maps v2 API 中是否有任何官方的地理编码示例?

4

2 回答 2

3

Geocoder与 Google Maps Android API v2 无关。

您可能希望直接使用Google Geocoding API而不是Geocoder,这会为您提供有限的数据量,并且可能会受到设备或 Android 版本特定问题的影响。

于 2013-05-20T18:36:26.333 回答
2

尝试这个 .....

      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));
于 2014-02-27T07:14:52.317 回答