2015-03-10 17 views
15

Sto tentando di seguire l'esercitazione Android Receiving Location Updates. Il codice di esempio è here on Github.Android non può risolvere il metodo requestLocationUpdates FusedLocationProviderAPI

Tutto funziona tranne che per questa linea: LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

Sta gettando un errore di cannot resolve method requestLocationUpdates. Non vedo nulla nel documentation, ma sono abbastanza nuovo per Android, quindi potrebbe mancare qualcosa :).

Ecco il codice completo. Qualche idea su dove sto andando male? Grazie!

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(); 
    } 

} 

risposta

65

Devi usare LocationListener per LocationServices.FusedLocationApi.requestLocationUpdates's locationListener

non

android.location.LocationListener

ma

com.google.android.gms.location.LocationListener

+2

dovrebbe avere notato che uno! Bella scoperta –

+0

Bella. Il LocationListener di questo problema dovrebbe essere cambiato. http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi – RoCk

+0

Uomo, sei geniale. Grazie –

0

importazione android.location.LocationListener o com.google.android.gms.location.LocationListener è inutile come i pacchetti rimane inutilizzato. Implementare l'interfaccia com.google.android.gms.location.LocationListener.

6

Import

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

invece di

import android.location.LocationListener; 
0

Invece di cambiare l'istruzione import, Usa implementa com.google.android.gms.location.LocationListener

di solo LocationListener/android.location.LocationListener.

Di sicuro ha risolto il mio problema.

+0

Spiega la tua risposta: in che modo l'implementazione di questa interfaccia risolverà l'errore? –

0

necessari Aggiornamento della versione di Google Play Services 10.0.0+

compile 'com.google.android.gms:play-services:10.0.0' 
Problemi correlati