2012-05-08 69 views
6

我想從Geocoder獲取位置地址,我可以獲取經度和緯度,但它返回的地址長度爲零。Android地址解析器返回空地址

這裏是我的代碼片斷

  double longi = location.getLongitude(); 
      double latt = location.getLatitude(); 

      Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH); 

      String add=""; 
      try { 


       List<Address> address = geocoder.getFromLocation(latt/1E6,longi/1E6, 1); 


       System.out.println("in location listener "+address.size()); 


       int i=0; 

       if(address.size()>0) 
       { 
        for(i=0;i<address.get(0).getMaxAddressLineIndex();i++) 
        { 
         add += address.get(0).getAddressLine(i); 
        } 


        Toast toast = new Toast(getApplicationContext()); 
        toast.makeText(getApplicationContext(), add, Toast.LENGTH_LONG).show(); 
       } 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

請幫我把它正確。提前致謝!

+1

[看一看這個答案](http://stackoverflow.com/a/9075559/593709) –

回答

6

如果平臺中沒有後端服務,Geocoder查詢方法將返回一個空列表。使用isPresent()方法來確定Geocoder實現是否存在。

查看Geocoder documentation瞭解更多信息。

+0

我從來沒有與地理編碼API一個愉快的經歷。但這是你最好的選擇。 – JustinDanielson

+5

我嘗試過'getFromLocationName()'與不同的地址。對於某些地址,我得到了結果,但對於某些地址,我得到了大小爲** 0 **的數組。 –

+1

我已經檢查了Geocoder.isPresent(),這個靜態函數返回true。然而,我得到空的地址列表 –

0

我認爲你的項目沒有針對正確的AVD。要使用地理位置,你必須使它指向實施谷歌API的AVD。一旦你改變了它,一切都會正常工作。

Installing the Google APIs Add-On