0

I am obtaining location with FusedLocationProvider and trying to get complete address from latitude and longitude. I get correct values but when i pass them to geocoder.getFromLocation they change to some russian place. Am i doing something wrong or is it just geocoder having problems?

locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult?) {
        locationResult ?: return
        for (location in locationResult.locations) {

            latitude = location.latitude
            longitude = location.longitude
            Log.d("location","latitude: $latitude")
            Log.d("location","longitude: $longitude")

            addresses = geocoder.getFromLocation(latitude,longitude,1)
            Log.d("location",addresses.toString())
            address = addresses[0].getAddressLine(0)
        }
    }
}

Logs:

D/location: latitude: 54.3835834
D/location: longitude: 18.586494
D/location: [Address[addressLines=[0:"Obwód wołogodzki, Rosja, 161445"],feature=161445,admin=Vologda Oblast,sub-admin=Nikolsky District,locality=null,thoroughfare=null,postalCode=161445,countryCode=RU,countryName=Rosja,hasLatitude=true,latitude=59.432300399999995,hasLongitude=true,longitude=44.93297,phone=null,url=null,extras=null]]
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Where is the `geocoder` object initialised? What API are you using? – aliaksei Nov 24 '19 at 16:51
  • Make sure the coordinates are of type `double` (which is not so obvious from the question). This location should be `Karola Szymanowskiego 33, 80-280 Gdańsk, Polska`. – Martin Zeitler Nov 24 '19 at 17:04
  • I initialize geocoder when declaring it as a variable `private val geocoder= Geocoder(this, Locale.getDefault())`, maybe i should do that in onStartCommand(), i forgot to mention it's a service. The type is double and the location is right, i am using API 28 – Wiktor Pieńkowski Nov 24 '19 at 17:23
  • From the javadocs: "The results are a best guess and are not guaranteed to be meaningful or correct. ". –  Nov 24 '19 at 19:18

0 Answers0