在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
现在包含街道名称,波特兰广场。 getAddressLine(0)
返回波特兰广场,5 Calverley St,Leeds LS1 3DA,英国,其中包含正确的城市利兹。我可以从getAddressLine(0)中删除街道名称,邮政编码和门牌号码,以便留在城市中。不幸的是,有些边缘情况不起作用,因为有时街道名称包含getAddressLine(0)
中的缩写但不包含thoroughfare
中的缩写。
这个问题How to Get City Name by Latitude &Longitude in android?的答案完全不同,因为不同国家的地址以不同的方式存储。
似乎有很多变化和边缘情况我需要编码才能获得英国2个邻近城市的城市名称。如何从世界范围内的任何纬度或经度获得正确的城市名称?世界上有太多的城市要测试我的代码和代码,并且可能有许多边缘情况和变化我甚至不知道存在。
编辑
以下功能正确地获得了英国的城市。不幸的是,它在美国和印度以及任何拥有国家的国家都失败了。
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);
private void getPlaceInfo(double lat,double lon)抛出IOException {
mGeocoder = new Geocoder(AddressSelectorActivity.this, Locale.ENGLISH);
List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 1);
if (addresses.get(0).getPostalCode() != null) {
String ZIP = addresses.get(0).getPostalCode();
Log.d("ZIP CODE",ZIP);
google_add_pincode.setText(ZIP);
}
if (addresses.get(0).getLocality() != null) {
String city = addresses.get(0).getLocality();
Log.d("CITY",city);
}
if (addresses.get(0).getAdminArea() != null) {
String state = addresses.get(0).getAdminArea();
Log.d("STATE",state);
}
if (addresses.get(0).getCountryName() != null) {
String country = addresses.get(0).getCountryName();
Log.d("COUNTRY",country);
}
}