2010-11-23 93 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 只需用你的地址替換那裏的地址即可。

好運