Android cannot resolve method requestLocationUpdates FusedLocationProviderAPI
Asked Answered
G

6

16

I'm attempting to follow the Android Receiving Location Updates tutorial. The example code is here on Github.

Everything is working except for this line: LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

It's throwing a cannot resolve method requestLocationUpdates error. I don't see anything in the documentation, but I'm pretty new to Android so might be missing something :).

Here's the full code. Any ideas where I'm going wrong? Thanks!

package location.test.example.com;

import android.app.Fragment;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.util.ArrayList;
import java.util.Date;

public class LocationFragment extends Fragment implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    protected GoogleApiClient mGoogleApiClient;
    protected Location mCurrentLocation;
    protected LocationRequest mLocationRequest;
    protected String mLastUpdateTime;

    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 5000;

    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLastUpdateTime = "";

        buildGoogleApiClient();

        if (NavUtils.getParentActivityName(getActivity()) != null) {
            getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setHasOptionsMenu(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_location, container, false);    

        return v;
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        createLocationRequest();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();

        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        startLocationUpdates();
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getDateFormat(getActivity()).format(new Date());
        Toast.makeText(getActivity(), location.toString() + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnected(Bundle connectionHint) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i("LocationFragment", "Connection failed: ConnectionResult.getErrorCode() " + result.getErrorCode());
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.i("LocationFragment", "Connection suspended");
        mGoogleApiClient.connect();
    }

}
Glacialist answered 10/3, 2015 at 0:52 Comment(0)
K
75

You have to use LocationListener for LocationServices.FusedLocationApi.requestLocationUpdates's locationListener

not

android.location.LocationListener

but

com.google.android.gms.location.LocationListener

Kries answered 16/3, 2015 at 5:8 Comment(7)
Should have noticed that one! Nice findGlacialist
Nice one. The LocationListener of this problem should be changed. #34582870Indianapolis
Man, you are brilliant. ThanksImplosive
will this work if user doesn't have google play services installed?Isomerize
Genius from hellNonah
I'm using com.google.android.gms.location.LocationListener but still have the same problem :(Bicarb
Even if I imported the right one, I'm still getting an error: "error: no suitable method found for requestLocationUpdates(GoogleApiClient,MapsActivity) LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, this);"Unshackle
M
11

Import

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

instead of

import android.location.LocationListener;
Mohock answered 10/1, 2017 at 11:35 Comment(1)
perfect solution. Thank you so muchNonparticipation
C
4

Replace

import android.location.LocationListener;

From

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

and remove these override methods

public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
Covenantee answered 7/8, 2018 at 9:16 Comment(0)
T
0

Importing android.location.LocationListener or com.google.android.gms.location.LocationListener is useless as the packages remains unused. Implement com.google.android.gms.location.LocationListener inteface will do.

Toreutic answered 12/3, 2016 at 16:40 Comment(0)
C
0

Instead of changing the import statement , Use implements com.google.android.gms.location.LocationListener

than just LocationListener/android.location.LocationListener.

It sure solved my problem.

Cockahoop answered 30/1, 2017 at 21:31 Comment(1)
Please explain your answer. How implementing this interface will fix the error?Equiangular
H
0

need update version of google play services 10.0.0+

compile 'com.google.android.gms:play-services:10.0.0'
Harcourt answered 22/6, 2017 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.