2015-10-19 88 views
0

我正在Android的GoogleMap上工作。「如何從LatLong獲取實際的文本值(地名)?」

到目前爲止,我已經獲得了當前位置並在其上顯示了標記。

我從當前位置獲得LatLong值。

我可以使用下面的代碼獲取城市名稱:

  Geocoder gcd = new Geocoder(MainActivity.this, Locale.getDefault()); 
     List<Address> addresses = null; 
     try { 
      addresses = gcd.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (addresses.size() > 0) 
      System.out.println(addresses.get(0).getLocality()); 
      Toast.makeText(MainActivity.this,""+addresses.get(0).getLocality(),Toast.LENGTH_LONG).show(); 
    } 

但是,不能得到atual區域名稱即Bodakdev字符拉斯塔,艾哈邁達巴德。

現在,我的問題是:如何從LatLong獲取實際文本值(地點名稱)?

回答

1

您可以通過您的谷歌地圖中的Geocoder對象獲取此內容。方法getFromLocation(double, double, int)做的工作。

Geocoder geocoder; 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 

addresses = geocoder.getFromLocation(latitude, longitude, 1); // 1 represent max location result to returned, by documents it recommended 1 to 5 

String address = addresses.get(0).getAddressLine(0); // If any additional  address line present than only, check with max available address lines by getMaxAddressLineIndex() 
String city = addresses.get(0).getLocality(); 
String state = addresses.get(0).getAdminArea(); 
String country = addresses.get(0).getCountryName(); 
String postalCode = addresses.get(0).getPostalCode(); 
String knownName = addresses.get(0).getFeatureName(); 
+0

雅我可以用上面的代碼的城市名稱,但如果我想要做如上的修改建議是什麼? –

+0

請參閱我編輯的答案。這會給你所有的細節。如果信息不適用於該輸入集,則爲NULL。 @JaiminModi –

0

使用此,

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1); 
String cityName = addresses.get(0).getAddressLine(0); 
String stateName = addresses.get(0).getAddressLine(1); 
String countryName = addresses.get(0).getAddressLine(2); 
String address = addresses.get(0).getAddressLine(0); 
+0

感謝它的正常工作 –

相關問題