0

I am developing an Android application (using Kotlin) that needs to access the user's location every 2-3 seconds. I've been researching (and testing) in the past few days for solutions, but I only seem to find examples in Java (that can't easily be translated to Kotlin). I wasn't able to understand it from Android's tutorials either.

What I have already done:

  • Asked for permissions (foreground & background GPS position access)
  • Made sure they've been accepted before trying to use it
  • Implemented the onLocationChanged() method
  • Accessed the last location (and used it)

I would like to update the location so that, by accessing the last location, I can use the newest location that Android knows.

This is something that I've tried, which I thought might be closer to the actual solution (but that is definitely wrong):

val locationListener: LocationListener = this
locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 5000, 10, locationListener
)

What is the correct way to do this? Thank you.

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31
  • "I am developing an Android application (using Kotlin) that needs to access the user's location every 2-3 seconds" -- there is no guarantee that you can get a location, let alone with updates that frequently. "I wasn't able to understand it from Android's tutorials either" -- you might want to ask questions about what you did not understand. "This is something that I've tried, which I thought might be closer to the actual solution (but that is definitely wrong)" -- it is conceivably correct, but we do not know what your results were. – CommonsWare Oct 23 '21 at 20:39
  • FWIW, [this sample project](https://gitlab.com/commonsguy/cw-android-r/-/tree/vFINAL/BackgroundLocation) shows getting location in the background -- it is covered in [this book](https://commonsware.com/R). [Here is another sample project](https://gitlab.com/commonsguy/cw-android-q/-/tree/vFINAL/LocationForeground), for using an activity and a foreground service -- that one is covered in [this book](https://commonsware.com/Q). – CommonsWare Oct 23 '21 at 20:40
  • @CommonsWare The result of the code I have shown in my question is that Android Studio gives me an error saying `None of the following functions can be called with the argument supplied`. *this* is written inside an activity that implements LocationListener (and others). What might be causing that? – Enrico Cortinovis Oct 24 '21 at 04:56
  • [The third parameter to your `requestUpdates()` call takes a `Float`](https://developer.android.com/reference/kotlin/android/location/LocationManager#requestlocationupdates). So, try changing `10` to `10f`. – CommonsWare Oct 24 '21 at 10:58
  • @CommonsWare Thank you I had tried that (and it said 10f was good), but for some reason it still does not accept the fourth argument! – Enrico Cortinovis Oct 24 '21 at 19:20
  • 1
    Is the `import` for `LocationListener` referring to `android.location.LocationListener`, or perhaps is it referring to something else named `LocationListener`? You need to pass a `android.location.LocationListener` as the fourth argument. – CommonsWare Oct 24 '21 at 19:54
  • 1
    @CommonsWare Thank you so much! As you suggested, I had accidentally imported `com.google.android.gms.location.LocationListener`, and not `android.location.LocationListener`. The code I had perfectly works now. – Enrico Cortinovis Oct 25 '21 at 05:52

2 Answers2

0

This worked for me, do not forget to ask for gps permission first:

package com.example.gpsexample

import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.location.LocationManager

class LocationUpdates(context: Context) {

    private val mGpsLocationClient: LocationManager =
        context.getSystemService(LOCATION_SERVICE) as LocationManager

    @SuppressLint("MissingPermission")
    fun start(){
        mGpsLocationClient.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0L ,
            0f ,
            locationListener

        )
    }

    private val locationListener =
        android.location.LocationListener { location -> //handle location change
            println(location)
        }



}
programandoconro
  • 2,378
  • 2
  • 18
  • 33
-1

First of all you can use java codes in Kotlin .Android Studio will automatically convert java to kotlin if you copy paste java code.

for google map update you use new google map api. Check these links to implement new google map api request : https://stackoverflow.com/a/62155149/16728174 https://developers.google.com/maps/documentation/android-sdk/start

Then for location update use Interval:

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5);
mLocationRequest.setFastestInterval(1);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

check this link to get information about setFastestInterval(); and setInterval(); differences: https://stackoverflow.com/a/26407873/16728174

user16728174
  • 182
  • 1
  • 4
  • Thank you for the answer. I know Android Studio automatically converts java code to kotlin code when something is pasted in the editor, and that I can also use java files with other kotlin files. But, for example, when I paste your code in, it shows an error on LocationRequest(), like in [this](https://drive.google.com/file/d/1vqcsRKI3BjDhiPJ60iXW1notwVTsxwhP/view?usp=sharing) image. And I would prefer not to change the code to use the new google map api (also, I don't need to show a map. I just need to get the position), but just request for location updates! Thank you. – Enrico Cortinovis Oct 23 '21 at 20:14
  • well, please check these answers . Hope these information help you:https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – user16728174 Oct 23 '21 at 20:26
  • I have already tried this too, but converting to kotlin and trying to adapt it does not work in this case either... – Enrico Cortinovis Oct 23 '21 at 20:27
  • Witch part of it does not work ? please clarify your question in details with screenshots or pasting your code here and others could help too .Do you have Runtime error(exception and ...) or your code does not even run on device? – user16728174 Oct 23 '21 at 20:41
  • I added a comment under the main question now, explaining that... "The result of the code I have shown in my question is that Android Studio gives me an error saying `None of the following functions can be called with the argument supplied`. *this* is written inside an activity that implements LocationListener (and others). What might be causing that?" - The code I showed in the question comes from that exact answer you gave me the link to. I'm not sure what the problem can be. – Enrico Cortinovis Oct 24 '21 at 04:59