我正在編程一個Android應用程序,將動態可用的緯度和經度座標轉換爲人類可讀的位置。如何將GPS座標轉換爲地方
例如,12.2,4.5位於英國倫敦市中心。關於我希望能夠找到的城市 - >城鎮的粒度。或者,如果沒有,至少該城市,
有人可以請告知什麼解決方案可用於這個問題。
我正在編程一個Android應用程序,將動態可用的緯度和經度座標轉換爲人類可讀的位置。如何將GPS座標轉換爲地方
例如,12.2,4.5位於英國倫敦市中心。關於我希望能夠找到的城市 - >城鎮的粒度。或者,如果沒有,至少該城市,
有人可以請告知什麼解決方案可用於這個問題。
嘗試這種情況:
//listenner location changed
private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
}
}
}
//Get address base on location
try{
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextfieldname.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
Log.d(TAG,addresses.get(0).getFeatureName() + ",
" + addresses.get(0).getLocality() +",
" + addresses.get(0).getAdminArea() + ",
" + addresses.get(0).getCountryName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
點位置(緯度,經度)轉換成可讀的地址或地名的過程稱爲Reverse GeoCoding。 [來自Wikepedia]
你必須利用GeoCoder類和使用方法getFromLocation。此方法返回List<Address>
,您可以通過迭代列表中的每個Address對象來訪問該方法。
例子:
我認爲這會給出一個更好的結果:
private String convertLocationToAddress(Location location) {
String addressText;
String errorMessage = "";
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
} catch (IOException ioException) {
// Network or other I/O issues
errorMessage = getString(R.string.network_service_error);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Invalid long/lat
errorMessage = getString(R.string.invalid_long_lat);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// No address was found
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
addressText = errorMessage;
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<>();
// Fetch the address lines, join them, and return to thread
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
addressText =
TextUtils.join(System.getProperty("line.separator"),
addressFragments);
}
return addressText;
}
你有兩個正確的答案。將答案標記爲對您有幫助的正確答案。 –