[英]Get address from latitude/longitude Android?
因此,我嘗試了在線上和StackOverflow上找到的許多不同解決方案,但似乎無法正常工作。 這是我目前正在使用的類似SO上的文章。 我究竟做錯了什么?
public void getLocationName() {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(49.123124, -122.652404, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", "" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Cannot get Address!");
}
Toast.makeText(getApplicationContext(), "Distance between two locations is: " + strAdd.toString(), Toast.LENGTH_LONG).show();
}
它總是拋出異常,所以它是我的坐標嗎?
這是我擁有的有效代碼,請嘗試一下。
public void GiveResult() {
Geocoder myLocation = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> myList = myLocation.getFromLocation(latitude,
longitude, 1);
if (myList != null && myList.size() > 0) {
Address address = myList.get(0);
result = address.getLocality();
}
Iterator<Address> it = myList.iterator();
while (it.hasNext()) {
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("Data is" + it.next());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
嘗試這個,
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.