1

I want to create a current location button to get the current address of the user. Currently I am using fusedLocationClient.lastLocation but its slow and sometimes it does not work.

Is there a better and updated way to get the current address from the user in 2023 using Kotlin.

Have a nice day!

miguev
  • 4,481
  • 21
  • 41
Cipri
  • 57
  • 1
  • 9
  • It would be more correct to use `fusedLocationClient.currentLocation`, but that is potentially even slower. – Tenfour04 Jan 27 '23 at 21:10
  • you are right. Is there other options? How does the other app use it and it works fine? Like Bolt – Cipri Jan 27 '23 at 21:42
  • The best way to get the current address location in Koltin 2023 is the same as the best way to get the current address location in Kotlin 2022. – rtsketo Jan 27 '23 at 22:20
  • could you explain the method please? @rtsketo – Cipri Jan 27 '23 at 22:23
  • To get an address requires a latlon and a API key. gAPI key is free for maybe 10,000 calls per month then you have to pay for it :( To get a lat lon you can use the gps device or you can get a latlon from fusedlocation. Fused location only works when the phone has wifi or phone data. – danny117 Jan 28 '23 at 18:57
  • 1
    I already have the lat and long, I want to get the street address from it. Currently I am using geocoder.getFromLocation(latitute,longitude,1) But is unstable and does not work sometimes. Is there anything else that I can use? – Cipri Jan 28 '23 at 19:20

2 Answers2

1

The last comment (Jan 28) makes this question clear enough to answer:

  1. A lat,lng location is already available, that is the starting point.
  2. The goal is to obtain the the... a street address.
  3. Currently this is done using geocoder.getFromLocation(latitute,longitude,1)
  4. The problem is that this method is unstable and does not work sometimes.
  5. The question is then: Is there anything else that I can use?

And the answer is: yes, with caveats.

First, please note that geocoder.getFromLocation() is provided by Android, for free but without guarantees:

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do not use this API for any safety-critical or regulatory compliance purpose.

Since your concern seems to be the reliability of this service, a better solution may be provided by the Google Maps Platform Geocoding API, which does offer certain guarantees of latency and availability, as it is one of the Google Maps Platform Core Services.

The first caveat of this solution is that it is not entirely free of charge, see here How the Geocoding API is billed and consider what the cost would be for based on your user base and how often a latlng needs to be converted to an address. Make sure to restrict your API keys and optimize your usage.

The second caveat, which you might even like, is that this API doesn't return just one address, it tends to return a few. You can try this out using the Geocoder Tool at https://developers.google.com/maps/documentation/utils/geocoder (click on the map and it will reverse geocode that point).

miguev
  • 4,481
  • 21
  • 41
  • Thanks for your answer. Any idea how I can use it in Kotlin? – Cipri Feb 15 '23 at 19:05
  • Also, I am calling the API, like: "https://maps.googleapis.com/maps/api/geocode/json?latlng=123,123&key=123" and it takes more then 10 seconds to retrieve the value. – Cipri Feb 15 '23 at 19:55
  • I'm not familiar with Kotlin but I can assure you 10 seconds is much higher than 10x the typical response time for the Geocoding API, you should be receiving results very nearly always within half a second. – miguev Feb 15 '23 at 21:53
  • Any idea why it takes more than 10 seconds for me? – Cipri Feb 16 '23 at 17:31
  • Sounds like a networking issue somewhere between you and the Google network, it could be anywhere. The first thing I'd try is measuring response times from multiple places in different networks, but that's only one starting point for troubleshooting your network and it might not be the best one. – miguev Feb 17 '23 at 17:46
  • As you said, it was a location problem. Thanks! Any idea how I can parse the big json that I receive? – Cipri Feb 20 '23 at 18:03
  • There are plenty of JSON parsers, just use whichever you find for your programming language of choice. See also https://developers.google.com/maps/documentation/geocoding/web-service-best-practices (there is a section about parsing responses, but the entire document is a highly recommended read). – miguev Feb 20 '23 at 22:33
0

Take a look at simple and step by step Google code lab:

https://codelabs.developers.google.com/codelabs/while-in-use-location/#1

You can also use and modify their complete template where is available on GitHub

C.F.G
  • 817
  • 8
  • 16