2013-05-20 21 views
1

的方法Geocoder.getFromLocationName()拋出異常Service not available在Android 4.1中,即使GooglePlayServicesUtil.isGooglePlayServicesAvailable()回報SUCCESSGeocoder.isPresent()回報true
新的Google Maps v2 API中是否有地理編碼的官方示例?Geocoder.getFromLocationName()拋出「服務不可用」異常在Android 4與谷歌地圖V2

+0

我發現這是一個解決辦法:重新啓動設備。但它不是一個解決方案... – UmbySlipKnot

+0

我仍然在找到解決這個錯誤的方法。有任何想法嗎? – UmbySlipKnot

+0

有什麼想法?似乎Geocoder無法在Android 4上運行,Google Maps V2 – UmbySlipKnot

回答

3

Geocoder與Google Maps Android API v2無關。

您可能希望直接使用Google Geocoding API而不是Geocoder,這會給您提供有限的數據量,並可能受設備或Android版本特定問題的影響。

+0

我不知道Geocoder與v2 API無關。 無論如何,我有沒有使用[this](https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests)方法? – UmbySlipKnot

+0

Geocoder類是否有任何改進? – UmbySlipKnot

+0

即使使用Google Geocoding API,我也看到了問題,因此我必須實施包含Geocoder類的解決方案。有任何想法嗎? – UmbySlipKnot

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)); 
+0

非常感謝,它非常完美。只是想現在,如果名稱有空格,它會崩潰。因此在執行避免崩潰的請求之前,編輯代碼以在'placesName'中用'%20'替換空白是個不錯的主意 – Youssef