[英]Can't get address from longitude and latitude in android
我試圖從經度和緯度獲取地址,但是當我這樣做時,它無效。 它在logcat中的teamtreehouse.com.stromy.MainActivity.getCompleteAddressString(MainActivity.java:163)上顯示W / System.err:
這是我嘗試過的方法
private String getCompleteAddressString() {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 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", "Canont get Address!");
}
return strAdd;
}
我這樣稱呼它
public void updateDisplay() {
mLocation.setText(getCompleteAddressString());
}
在getCompleteAddressString()方法中,我使用了經度和緯度,這是我已經從另一個方法獲得的,並且可以正常工作。 兩種方法都以main活動運行。 除此之外,其他所有東西都工作正常,我該如何解決? 此錯誤的原因是什么?
像這樣嘗試:
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
Address addr = null;
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
addr = addresses.get(0);
String info = "Address is: ";
info += addr.getMaxAddressLineIndex() > 0 ? addr
.getAddressLine(0) : "";
info = info + ", " + addr.getLocality() + ", "
+ addr.getCountryName();
Toast.makeText(getApplicationContext(), info,
Toast.LENGTH_LONG).show();
} else
Toast.makeText(getApplicationContext(),
"Address not found", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Address not found",
Toast.LENGTH_LONG).show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.