我最近开始在Android上编程。在我的应用程序中,我想告诉用户他们的位置用文字,例如:“华盛顿”或“纽约”。我已经看到我可以得到纬度和经度,但我想用文字告诉它的位置(探究一个字符串)。
我可以这样做吗?如果我能,怎么样?
准确度是多少?它能给我这个城市的名字还是只有州名?
感谢您的帮助!
答案 0 :(得分:1)
以下是使用Thread和Handler获取Geocoder答案而不会阻止UI的完整示例代码。
Geocoder调用程序,可以位于Helper类
中public static String getAddressFromLocation(final Location location, final Context context, final Handler handler)
{
Thread thread = new Thread() {
@Override public void run()
{
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (list != null && list.size() > 0)
{
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality()+","+address.getCountryName();
}
}
catch (IOException e)
{
Log.e(TAG, "Impossible to connect to Geocoder", e);
}
}
};
thread.start();
return result;
}
答案 1 :(得分:1)
您可以使用反向geocoder来实现它。该示例代码:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latValue,
longValue, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder(
"Address:\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
myAddress.setText(strReturnedAddress.toString());
} else {
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
在获得纬度和经度值后,只需放置此代码即可。
答案 2 :(得分:-2)