2010-11-23 87 views
15

我试图获取我当前位置的街道名称,但似乎无法获取它。从Android中的地址/位置对象获取街道名称

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

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

如何检索我目前所在职位的街道名称?

回答

13

您是否尝试过使用getAddressLine? 更多信息,请参见here这种方法

像这样的东西应该做的(未经测试):

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

呀,像我上面说的,当我打印getAddressLine(0)和(1)我得到的邮递区号,城市和国家。没有街道名.. – Galip 2010-11-23 14:23:26

+0

对不起,我没有注意到它。也许你可以尝试通过模拟器发送另一个位置的经纬度,并检查是否可以看到一些返回的街道名称。 – ccheneson 2010-11-23 14:27:51

+0

也许你也可以增加maxResult来检查其他结果包含的信息。 – ccheneson 2010-11-23 14:29:47

-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()返回Streetname
  • getlocality()的返回城市
  • getadminarea()返回状态

这就是全部..!

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

你应该能够适应这让街道名称进行而不会引起大惊小怪。

Inspired by this answer

1

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

address.getAddressLine(0) 

找到街道名称和门牌号。

8

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

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

它为我工作:-)祝你好运;-)

1

getFromLocation没有工作对我也。有几个步骤可以采取。

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

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

3.试试在线api: http://maps.google.com/maps/api/geocode/json?address=192%20McEwan%20Dr%20E,%20Caledon,%20ON&sensor=false 只需用你的地址替换那里的地址即可。

好运