gpt4 book ai didi

java - 如何从地理编码器获取城市名称?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:19 25 4
gpt4 key购买 nike

在 Android 中(Java 或 Kotlin 都可以)如何从经度和纬度获取城市名称?

下面我编写了一个显示地址数据的函数。我想修改它,以便它返回城市名称:

private fun getCity(context: Context, latitude: Float, longitude: Float) {
val gcd = Geocoder(context, Locale.getDefault())
val addresses = gcd.getFromLocation(latitude.toDouble(), longitude.toDouble(), 1)
Log.d("-----", "-----")
Log.d("latitude", latitude.toString())
Log.d("longitude", longitude.toString())
if (addresses.size > 0) {
val locality = addresses[0].locality
if (locality != null) {
Log.d("locality", locality.toString())
}
val adminArea = addresses[0].adminArea
if (adminArea != null) {
Log.d("adminArea", adminArea.toString())
}
val subAdminArea = addresses[0].subAdminArea
if (subAdminArea != null) {
Log.d("subAdminArea", subAdminArea.toString())
}
val subLocality = addresses[0].subLocality
if (subLocality != null) {
Log.d("subLocality", subLocality.toString())
}
val thoroughfare = addresses[0].thoroughfare
if (thoroughfare != null) {
Log.d("thoroughfare", thoroughfare.toString())
}
val subThoroughfare = addresses[0].subThoroughfare
if (subThoroughfare != null) {
Log.d("subThoroughfare", subThoroughfare.toString())
}
val extras = addresses[0].extras
if (extras != null) {
Log.d("extras", extras.toString())
}
val premises = addresses[0].premises
if (premises != null) {
Log.d("premises", premises.toString())
}
val postalCode = addresses[0].postalCode
if (postalCode != null) {
Log.d("postalCode", postalCode.toString())
}
val featureName = addresses[0].featureName
if (featureName != null) {
Log.d("featureName", featureName.toString())
}
val getAddressLine0 = addresses[0].getAddressLine(0)
if (getAddressLine0 != null)
Log.d("getAddressLine0", getAddressLine0.toString())
for (i in 0 until addresses[0].maxAddressLineIndex) {
Log.d("getAddressLine($i)", addresses[0].getAddressLine(i).toString())
}
}
}

如果我运行 getCity(context, 53.7f, -1.506f)featureName 包含正确的城市名称 Wakefield。

如果我运行 getCity(context, 53.801f, -1.549f)featureName 现在包含街道名称 Portland Place。 getAddressLine(0) 返回 Portland Place, 5 Calverley St, Leeds LS1 3DA, UK,其中包含正确的城市利兹。我可以从 getAddressLine(0) 中删除街道名称、邮政编码和门牌号,以保留城市。不幸的是,在某些边缘情况下,这不起作用,因为有时街道名称在 getAddressLine(0) 中包含缩写,但在 thoroughfare 中不包含缩写。

这个问题How to Get City Name by Latitude &Longitude in android?答案完全不同,因为不同国家的地址存储方式不同。

为了获取英国两个邻近城市的城市名称,我需要编写相当多的变化和边缘情况。如何从世界各地的任何纬度或经度获得正确的城市名称?世界上有太多的城市需要测试我的代码和编码,并且可能存在许多我不知道的边缘情况和变化。

编辑

下面的函数可以正确获取英国的城市。不幸的是,它在美国和印度以及可能在任何有州的国家都失败了。

private fun getCity(context: Context, latitude: Float, longitude: Float): String {
val gcd = Geocoder(context, Locale.getDefault())
val addresses = gcd.getFromLocation(latitude.toDouble(), longitude.toDouble(), 1)
if (addresses.size > 0) {
val locality = addresses[0].locality
val getAddressLine0 = addresses[0].getAddressLine(0)
if (getAddressLine0 != null) {
val strs = getAddressLine0.split(",").toTypedArray()
if (strs.count() == 1) {
if (locality != null && locality != "") {
return "$locality, ${strs[0]}"
}
return strs[0]
}
for (str in strs) {
var tempStr = str.replace(postalCode, "")
if (tempStr != str) {
tempStr = tempStr.trim()
if (locality != null && locality != "" && locality != tempStr) {
return "$locality, $tempStr"
}
return tempStr
}
}
}
if (locality != null) {
return locality
}
}
return ""
}

最佳答案

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);

关于java - 如何从地理编码器获取城市名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55610188/

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