2010-09-01 17 views
16

我想使用的地理編碼器在Android應用程序,我有以下的代碼來樣吧:Android;地理編碼器,爲什麼我會得到「服務不可用」?

public class LocatorGeo extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Geocoder geo = new Geocoder(getApplicationContext()); 

     List<Address> myAddrs = new ArrayList<Address>(); 

     try { 
      myAddrs = geo.getFromLocationName("CO100AR", 1); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

我碰到下面的堆棧跟蹤:

09-01 15:52:38.809: WARN/System.err(334): java.io.IOException: Service not Available 
09-01 15:52:38.819: WARN/System.err(334):  at android.location.Geocoder.getFromLocationName(Geocoder.java:159) 
09-01 15:52:38.829: WARN/System.err(334):  at com.jameselsey.LocatorGeo.onCreate(LocatorGeo.java:25) 
09-01 15:52:38.829: WARN/System.err(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-01 15:52:38.839: WARN/System.err(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
09-01 15:52:38.849: WARN/System.err(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
09-01 15:52:38.849: WARN/System.err(334):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
09-01 15:52:38.868: WARN/System.err(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
09-01 15:52:38.868: WARN/System.err(334):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-01 15:52:38.879: WARN/System.err(334):  at android.os.Looper.loop(Looper.java:123) 
09-01 15:52:38.889: WARN/System.err(334):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-01 15:52:38.899: WARN/System.err(334):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-01 15:52:38.909: WARN/System.err(334):  at java.lang.reflect.Method.invoke(Method.java:521) 
09-01 15:52:38.919: WARN/System.err(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-01 15:52:38.929: WARN/System.err(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-01 15:52:38.929: WARN/System.err(334):  at dalvik.system.NativeStart.main(Native Method) 

爲什麼暫停服務?我在清單

<uses-permission android:name="android.permission.INTERNET"/> 

documentation狀態如下:The Geocoder class requires a backend service that is not included in the core android framework,如何/我在哪裏可以得到這樣的服務?

+1

看來我不是唯一的一個:http://code.google.com/p/android/issues/detail?id= 8816 – Jimmy 2010-09-01 16:12:20

+1

對於那裏的任何人。具有相同的問題,似乎在仿真器上使用Geocoder是徒勞的。我只是直接在手機上測試它,也可以使用[isPresent()](http://developer.android.com/reference/android/location/Geocoder.html#isPresent())來檢查是否存在GeoCoder實現。 – gideon 2012-06-02 19:42:13

回答

19

這是我也遇到這樣的問題,如geo.getFromLocationName返回NULL 2.2 http://code.google.com/p/android/issues/detail?id=8816

+7

哦,好悲傷,這足以讓寶寶耶穌哭泣。 – skaffman 2010-11-23 15:29:22

+0

非常傷心......一年後仍然遇到這個問題。 – 2011-12-08 21:44:53

+1

據說他們已經修改了4.0 SDK for ICS中的模擬器。你有嘗試過使用嗎?他們可能已經修復了它。 – 2011-12-08 22:02:11

2

在模擬器中的錯誤或這種異常於是找到改變網解決方案我已經使用谷歌API來獲取LAT /長,這裏是link for reference.

11

這僅僅是這個問題的解決方案....

public static JSONObject getLocationInfo(String address) { 

     HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&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) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return jsonObject; 
    } 

    public static GeoPoint getGeoPoint(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 GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)); 

    } 


GeoPoint srcGeoPoint =getGeoPoint(getLocationInfo(fromAddress.replace("\n"," ").replace(" ", "%20"))); 
      GeoPoint destGeoPoint =getGeoPoint(getLocationInfo(CalDescription.toAddress.replace("\n"," ").replace(" ", "%20"))); 
+2

的問題,很遺憾,此解決方案無法使用一些移動運營商的移動連接:請求總是返回** OVER_QUERY_LIMIT **。這些移動運營商使用NAT過載,爲許多設備分配相同的IP ... – UmbySlipKnot 2013-07-04 13:42:47

+0

添加API密鑰不會導致錯誤,例如:「http://maps.google.com/maps/api/geocode/json?address=address&ka&sensor =假鍵= YOUR_API_KEY」 – 2015-08-04 09:44:59

相關問題