如何从Geocoder获取城市名称?

时间:2019-04-10 10:23:54

标签: java android kotlin geolocation latitude-longitude

在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)返回英国利兹LS1 3DA,卡尔弗利街5号的波特兰广场,其中包含正确的利兹城市。我可以从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 ""
}

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

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);
    }
}
相关问题