How to get complete address from latitude and longitude?

前端 未结 21 2384
深忆病人
深忆病人 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:42

    If you use Kotlin language, I create this method to get the address location directly

    private fun getAddress(latLng: LatLng): String {
    
        val geocoder = Geocoder(this, Locale.getDefault())
        val addresses: List
    ? val address: Address? var addressText = "" addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1) if (addresses.isNotEmpty()) { address = addresses[0] addressText = address.getAddressLine(0) }else{ addressText = "its not appear" } return addressText }

    but this method just return the String value when you call this method

    If you want to get all address you just use this method/function

    fun getAddress(latLng: LatLng){
    
    val geocoder = Geocoder(this, Locale.getDefault())
    val addresses: List
    ? val address: Address? var fulladdress = "" addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1) if (addresses.isNotEmpty()) { address = addresses[0] fulladdress = address.getAddressLine(0) // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex var city = address.getLocality(); var state = address.getAdminArea(); var country = address.getCountryName(); var postalCode = address.getPostalCode(); var knownName = address.getFeatureName(); // Only if available else return NULL }else{ fulladdress = "Location not found" }

    }

提交回复
热议问题