gpt4 book ai didi

android - 如何在 google map android v2 的 map 上获取城市名称 ontap?

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:03 25 4
gpt4 key购买 nike

通过点击 map ,我怎样才能得到那个位置的地址。在特定位置点击我们可以监听并传递我们可以获得地址的坐标,但它应该在 android 谷歌地图 v2 中工作。

最佳答案

如果您有 LatLng 数据 - 这很容易。您需要使用 Goecoder .

    protected String doInBackground(Location... params) {
Geocoder geocoder =
new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
Location loc = params[0];
// Create a list to contain the result address
List<Address> addresses = null;
try {
/*
* Return 1 address.
*/
addresses = geocoder.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
} catch (IOException e1) {
Log.e("LocationSampleActivity",
"IO Exception in getFromLocation()");
e1.printStackTrace();
return ("IO Exception trying to get address");
} catch (IllegalArgumentException e2) {
// Error message to post in the log
String errorString = "Illegal arguments " +
Double.toString(loc.getLatitude()) +
" , " +
Double.toString(loc.getLongitude()) +
" passed to address service";
Log.e("LocationSampleActivity", errorString);
e2.printStackTrace();
return errorString;
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
/*
* Format the first line of address (if available),
* city, and country name.
*/
String addressText = String.format(
"%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ?
address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
} else {
return "No address found";
}
}
...
}
...

最好的方法是使用 AsyncTask。您可以在 Android 开发人员的网站上找到完整的示例:http://developer.android.com/training/location/display-address.html

关于android - 如何在 google map android v2 的 map 上获取城市名称 ontap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com