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

17

我想获取当前位置的街道名称,但好像无法获得。

我使用以下方法检索地址:

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;
        }
    }

然后我可以执行诸如 address.getLocality()address.getPostalCode() 之类的操作。

但我想要的是街道名称,例如 "Potterstreet 12"。当我打印 AddressLine(0) 和 AddressLine(1) 时,我只得到邮政编码、城市和国家。

我该怎么获取我的当前位置的街道名称?

6个回答

14

你尝试使用getAddressLine了吗? 在这里可以查看更多关于该方法的信息。

像这样的代码应该可以(未经测试):

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

是的,就像我上面说的,当我打印getAddressLine(0)和(1)时,我得到了邮政编码、城市和国家。没有街道名称... - Galip
抱歉,我没有注意到。也许你可以尝试通过模拟器发送另一个位置的纬度和经度,并检查是否返回了一些街道名称。 - ccheneson
也许你可以增加maxResult来检查其他结果包含的信息。 - ccheneson
我已经增加了maxResult,但它并没有起作用。 我认为问题确实在于我的位置没有地址。顺便说一下,我是在手机上开发。 如果你想测试基于位置的应用程序,模拟器表现非常糟糕。 - Galip
这是我遇到的问题...正在寻找一种高效的方法,在地址对象的地址行数不总是相同的情况下获取国家名称。 - jsims281
显示剩余2条评论

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();

这对我有效 :-) 祝你好运 ;-)


2
getThoroughfare() 在郊区的情况下会返回 null。它不能泛化到用户的每个位置。 - Mrigank

1

1
如果您有完整的地址(城市+街道),在IT系统中输入它们可能会更容易。
address.getAddressLine(0)

您可以找到街道名称和门牌号码。


0

我曾经遇到过一个非常类似的问题,但是与国家名称有关,这是我最终使用的函数:

function getCountry(results) {
    var geocoderAddressComponent,addressComponentTypes,address;
    for (var i in results) {
      geocoderAddressComponent = results[i].address_components;
      for (var j in geocoderAddressComponent) {
        address = geocoderAddressComponent[j];
        addressComponentTypes = geocoderAddressComponent[j].types;
        for (var k in addressComponentTypes) {
          if (addressComponentTypes[k] == 'country') {
            return address.long_name;
          }
        }
      }
    }
   return 'Unknown';
 }

你应该能够轻松地适应这个方法,从中获取街道名称。 受这个答案的启发

-3
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = 
                gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0 && addresses != null) {
                StringBuilder result = new StringBuilder();
                myaddress.setText(addresses.get(0).getFeatureName()+"-"+addresses.get(0).getLocality()+"-"+addresses.get(0).getAdminArea()+"-"+addresses.get(0).getCountryName());
}
  • getfeaturename() 返回街道名称
  • getlocality() 返回城市
  • getadminarea() 返回州

就这些..!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接