0

I get this error when I run the code below:

None of the following functions can be called with the arguments supplied: 
public constructor FusedLocationProviderClient(p0: Activity) defined in com.google.android.gms.location.FusedLocationProviderClient
public constructor FusedLocationProviderClient(p0: Context) defined in com.google.android.gms.location.FusedLocationProviderClient

Code:

import android.location.Location
import android.os.Build
import android.os.SystemClock
import com.google.android.gms.location.FusedLocationProviderClient

class startMockLocation {

    fun main() {
        var locationProvider = FusedLocationProviderClient() // I GET THE ERROR HERE
        locationProvider.setMockMode(true)

        val loc = Location("gps")
        val mockLocation = Location("gps") // a string
        mockLocation.latitude = 48.8566  // double
        mockLocation.longitude = 2.3522
        mockLocation.altitude = loc.altitude
        mockLocation.time = System.currentTimeMillis()
        mockLocation.accuracy = 1f
        mockLocation.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mockLocation.bearingAccuracyDegrees = 0.1f
            mockLocation.verticalAccuracyMeters = 0.1f
            mockLocation.speedAccuracyMetersPerSecond = 0.01f
        }
//        locationManager.setTestProviderLocation(providerName, mockLocation)
        locationProvider.setMockLocation(mockLocation)
    }
}

How can I fix this error? I simply want to set the device's location to the given coordinates. I also get more issues but this is the main error.

1 Answers1

0

You need to pass the current activity context as a parameter:

var locationProvider = FusedLocationProviderClient(this);

The error is saying there are 2 constructors you could use, one with an Activity passed:

FusedLocationProviderClient(p0: Activity)

And one with Context passed:

FusedLocationProviderClient(p0: Context)

If that doesn't work, you could also try this, which I see used more often. I'm not sure about the differences between the two methods:

var locationProvider = LocationServices.getFusedLocationProviderClient(this);
amabe
  • 21
  • 1
  • 6
  • i now get error `Type mismatch: inferred type is startMockLocation but Activity was expected` –  May 17 '20 at 23:25
  • Try this@startMockLocation – amabe May 17 '20 at 23:29
  • i now get `'startMockLocation' is not an annotation class` –  May 17 '20 at 23:30
  • the rest of my android studio project is in Java btw –  May 17 '20 at 23:36
  • 1
    Ah, that changes it. You need to be able to pass Context or Activity. getApplicationContext() may work. – amabe May 17 '20 at 23:41
  • i tried `var locationProvider = FusedLocationProviderClient(getApplicationContext()) ` and i get `Unresolved reference: getApplicationContext` –  May 17 '20 at 23:49