2012-05-08 67 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