从Android中的地址/位置对象获取街道名称

Gal*_*lip 16 android google-maps geolocation

我正在尝试获取当前位置的街道名称,但我似乎无法得到它.

我用这个方法来检索地址:

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        if (addresses.size() == 1) {
            return addresses.get(0);
        } else {
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我可以做类似的事情.address.getLocality()address.getPostalCode()

但我想要的是街道名称.就像在"Potterstreet 12"中一样.当我打印AddressLine(0)和AddressLine(1)时,我只获得邮政编码,城市和国家.

如何检索我目前所处位置的街道名称?

cch*_*son 14

你试过用过getAddressLine吗?有关此方法的详细信息,请参见此处

这样的事情应该做(未经测试):

for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
 Log.d("=Adress=",addresses.getAddressLine(i));
}
Run Code Online (Sandbox Code Playgroud)


use*_*152 9

在你的代码中尝试这样的东西

String cityName=null;              
Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault());               
List<Address>  addresses;    
try {    
   addresses = gcd.getFromLocation(location.getLatitude(), location  
                   .getLongitude(), 1);    
   if (addresses.size() > 0) 
        StreetName=addresses.get(0).getThoroughfare();
        String s = longitude+"\n"+latitude +  
        "\n\nMy Currrent Street is: "+StreetName; 
         Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)

它对我有用:-)祝你好运;-)

  • 当你在郊区时,getThoroughfare()返回null.它不能推广到用户的每个位置. (2认同)