How to get complete address from latitude and longitude?

前端 未结 21 2130
深忆病人
深忆病人 2020-11-22 09:07

I want to get following values from Latitude and Longitude in android

  1. Street Address
  2. City / State
  3. Zip
  4. Complete Address
<
21条回答
  •  长情又很酷
    2020-11-22 09:24

    Its very easy to get complete address from the Latitude and Longitude using Geocoder class. Following the code sample. Hope this helps!

     if (l != null) {
            val lat = l.latitude
            val lon = l.longitude
    
            val geocoder = Geocoder(this, Locale.getDefault())
            val addresses: List
    addresses = geocoder.getFromLocation(lat, lon, 1) val address = addresses[0].getAddressLine(0) val address2 = addresses[0].getAddressLine(1) val city = addresses[0].locality val state = addresses[0].adminArea val country = addresses[0].countryName val postalCode = addresses[0].postalCode val knownName = addresses[0].featureName val message = "Emergency situation. Call for help. My location is: " + address + "." + "http://maps.google.com/maps?saddr=" + lat + "," + lon }

    You can use only the address value as it gives you all the complete address. If you want individual components, you can use others as well.

提交回复
热议问题