簡體   English   中英

Geocoder無法通過getfromlocationName工作

[英]Geocoder not working by getfromlocationName

我的代碼如下:

Geocoder geocoder = new Geocoder(this,Locale.UK );
         List<Address> addresses = null;
         Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 
   try {
    Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 

    addresses = geocoder.getFromLocationName( 
      "Camden", 1);

    if (addresses.size() > 0) {

           geopoint = new GeoPoint(
                         (int) (addresses.get(0).getLatitude() * 1E6), 
                         (int) (addresses.get(0).getLongitude() * 1E6));

           //Animating on display

           mapcontroller.animateTo(geopoint);
           mapcontroller.setZoom(5); 
           mapview.invalidate();
          } 


   } catch (IOException io) {
    // TODO Auto-generated catch block
    Toast.makeText(displaybyName.this, "Connection Error", Toast.LENGTH_SHORT).show();
   }

   if (addresses.size() > 0) {

          geopoint = new GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

          //Animating on display

          mapcontroller.animateTo(geopoint);
          mapcontroller.setZoom(5); 
          mapview.invalidate();
         } 

但是,該應用每次都崩潰。 地理編碼器未返回任何經度/緯度。

代碼有任何問題... plz plz建議。

仔細檢查一下

addresses = geocoder.getFromLocationName("Camden", 1);

返回不為null -否則:下一行將引發NullPointerException


進一步閱讀

存在android版本問題,因為geocoder並不包含核心的android框架,但是這在舊版本和新版本(例如1.6、2.0和API級別15)上都可以正常運行,請先嘗試這些功能,但仍會堅持使用,但仍然要堅持自己嘗試使用JSON的geocoder類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM