Franks
Franks

Reputation: 81

Error fusedLocationApi requestLocationUpdates() non-activity class error

Hi I'm new in stackoverflow. I hope someone Know how to do something in Android to call method requestLocationUpdates using FusedLocationApi.

I'm trying to call in a non-activity class that implements the classes nedded to request location updates: LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

But it shows message that "Cannot resolve method requestLocationUpdates(...)"

 /**
 * Requests location updates from the FusedLocationApi.
 */
protected void startLocationUpdates() {
  LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

And the non-activity class that I use implements ConnectionCallbacks, OnConnectionFailedListener and LocationListener and extends my activity class.

public class OnMapGps extends OnActivity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

    ...
    ...
}

The error that shows is:

Error:(306, 42) error: no suitable method found for requestLocationUpdates(GoogleApiClient,LocationRequest,OnMapGps)
method FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient,LocationRequest,LocationListener)
is not applecable (argument mismatch; OnMapGps cannot be converted to LocationListener) 

Upvotes: 0

Views: 3814

Answers (3)

Thilina Chamika
Thilina Chamika

Reputation: 320

[Solution]

import below

import com.google.android.gms.location.LocationListener;

Add the below permission to the manifest file, This is required. After that issues will be resolved.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Code

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);

Upvotes: 0

Amgad
Amgad

Reputation: 169

I resolved this error by casting 'this' into a 'LocationListener' Object

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);

Upvotes: 1

Franks
Franks

Reputation: 81

I found the solution, it seems that i had the wrong import LocationListener. I had

import android.location.LocationListener;

And the correct is

import com.google.android.gms.location.LocationListener;

I think that I had to ask to find the solution myself. :P

Upvotes: 5

Related Questions