0

I am getting the error None of the following functions can be called with the arguments supplied when trying to call requestLocationUpdates() using Kotlin. I've gotten this issue before in other places and have been able to fix it, but I'm not able to in this case. I'm relatively new to the language and don't fully understand the issue here.

The issue is with the LocationListener that is passed in. It is expecting a LocationListener!, but apparently that isn't what it's getting.

Here is the snippet of code:

    private var locationManager: LocationManager? = null
    private var latitude: Double? = null
    private var longitude: Double? = null
    //private val locationListener: com.google.android.gms.location.LocationListener

    private val locationListener: com.google.android.gms.location.LocationListener = object : com.google.android.gms.location.LocationListener {
        override fun onLocationChanged(location: Location) {
            latitude = location.latitude
            longitude = location.longitude
        }
    }

    init {
        // Initialize Places.
        Places.initialize(activity, BuildConfig.googlePlacesAPI_KEY)
        Places.createClient(activity)

        locationManager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager?

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 50f, locationListener)

        if (locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            getLocation(activity)
        }
    }
Cameron
  • 1,281
  • 1
  • 19
  • 40

1 Answers1

0

You are using the wrong LocationListener. Because you're not using the FusedLocationProviderClient you should be using the android.location.LocationListener instead and implement all methods.

private val locationListener = object : android.location.LocationListener {
    override fun onProviderEnabled(provider: String?) {}

    override fun onProviderDisabled(provider: String?) {}

    override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
        Log.d("status = $status")
    }

    override fun onLocationChanged(location: Location?) {
        latitude = location?.latitude
        longitude = location?.longitude
    }
}

I'd suggest you to use the fused location provider instead of the android location service because it is simpler to implement. In fact, it is exactly what Google recommends. See the developer guides below...

https://developer.android.com/training/location/receive-location-updates

Leo
  • 14,625
  • 2
  • 37
  • 55
  • That is not the issue. It is expecting a `LocationListener!` and apparently whatever it's getting passed is not that. I have edited my question – Cameron Aug 06 '19 at 02:23
  • @cpgreen2 I was just adding more details to the answer – Leo Aug 06 '19 at 02:24
  • What did you mean about using the wrong LocationListener? There is only one other one, and it does not have a constructor – Cameron Aug 06 '19 at 02:26
  • 2
    @cpgreen2 you shouldn't have any issues if you implement the correct `LocationListener` from the `android.location` package – Leo Aug 06 '19 at 04:01
  • Okay that's what I thought, I'm doing that, but in a different place. Thank you! – Cameron Aug 06 '19 at 04:42