【问题标题】:Getting street name from Address/Location object in Android从 Android 中的地址/位置对象获取街道名称
【发布时间】:2011-05-14 11:57:16
【问题描述】:

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

我使用这种方法来检索地址:

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) 时,我只得到邮政编码、城市和国家/地区。

如何检索我当前所在位置的街道名称?

【问题讨论】:

    标签: android google-maps geolocation


    【解决方案1】:

    您是否尝试过使用 getAddressLine ? 有关此方法的更多信息,请参阅here

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

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

    【讨论】:

    • 是的,就像我上面说的,当我打印 getAddressLine(0) 和 (1) 时,我得到了邮政编码、城市和国家。没有街道名称..
    • 对不起,我没注意到。也许您可以尝试通过模拟器发送另一个位置的一些纬度和经度,然后检查是否可以看到返回的一些街道名称。
    • 也许您还可以增加 maxResult 以检查其他结果包含的信息。
    • 我已经增加了 maxResult 并且它没有工作.. 我认为这确实是基于我的位置没有地址的事实。顺便说一句,我正在手机上开发。如果你想测试基于位置的应用程序,模拟器真的很糟糕。
    • 在我的 HTC Desire 中,在 Applications -> Development 下,我有一个名为“允许模拟位置”的选项。也许你可以激活它并将地理定位从 DDMS 发送到手机(虽然我从未尝试过)
    【解决方案2】:

    在你的代码中尝试这样的事情

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

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

    【讨论】:

    • getThoroughfare() 如果您在郊区,则返回 null。它不能推广到用户的每个位置。
    【解决方案3】:

    如果你有完整的地址(城市+街道),在

    address.getAddressLine(0)
    

    你找到街道名称和号码。

    【讨论】:

      【解决方案4】:

      getFromLocation 也不适合我。您可以采取几个步骤。

      1. 首先进入 gradle 并确保您使用的是最新的播放服务库。

      2. 不要过度指定,我没有得到结果的原因是因为我的地址中有很多信息。当我删除邮政编码时,我每次都会得到结果。

      3.尝试在线api: http://maps.google.com/maps/api/geocode/json?address=192%20McEwan%20Dr%20E,%20Caledon,%20ON&sensor=false 把里面的地址换成你的就行了。

      祝你好运

      【讨论】:

        【解决方案5】:

        我有一个非常相似的问题,但是国家名称,这是我最终使用的功能:

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

        您应该能够对其进行调整,以便毫不费力地获得街道名称。

        Inspired by this answer

        【讨论】:

          【解决方案6】:
          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() 返回状态

          就是这样......!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-08-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-26
            • 1970-01-01
            相关资源
            最近更新 更多