Android - 使用googleApiClient获取当前位置

时间:2016-04-06 15:17:54

标签: java android gps location google-api-client

对于我的项目,我需要每秒请求android的当前位置。

我在互联网上找到了一些代码,解释了如何使用GoogleApiClient来获得该职位。

对于我在文档中阅读的内容,当我选择locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)时,这意味着GPS坐标始终优先于WIFI定位。 从右吗

如果我停用WIFI 并只保留 GPS和GPRS启用,如果我去散步,我会得到一个完美的坐标集表明我的课程。

但是,如果我同时使用 WIFI和GPRS启用GPS ,我会选择相同的课程,那么我会得到更少的分数。

在描述的任何情况下,我不应该获得相同数量的坐标吗?

AndroidManifest.xml(权限)

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

CheckGeoLocation.java(获取位置代码的服务)

import android.location.Location;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


public class CheckGeoLocation extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static final String TAG = "LocationService";

    private boolean currentlyProcessingLocation = false;
    private LocationRequest locationRequest;
    private GoogleApiClient googleApiClient;

    @Override
    public void onCreate(){super.onCreate();}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        if (!currentlyProcessingLocation) {
            Log.d(TAG, "about to start tracking....");
            currentlyProcessingLocation = true;
            startTracking();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void startTracking() {
        Log.d(TAG, "startTracking");

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {

            googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) {
                googleApiClient.connect();
            }
        } else {
            Log.e(TAG, "unable to connect to google play services.");
        }
    }

    @Override
    public void onDestroy() {super.onDestroy();}

    @Override
    public IBinder onBind(Intent intent) {return null;}

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.d(TAG, "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy());
            new PostCoords(location).execute();
        }else{
            Log.d(TAG,"NO POSITION FOUND");
        }
    }

    private void stopLocationUpdates() {
        if (googleApiClient != null && googleApiClient.isConnected()) {
            googleApiClient.disconnect();
        }
    }

    /**
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected");

        locationRequest = LocationRequest.create();
        locationRequest.setInterval(1000); // milliseconds
        locationRequest.setFastestInterval(1000); // the fastest rate in milliseconds at which your app can handle location updates
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationServices.FusedLocationApi.requestLocationUpdates(
                googleApiClient, locationRequest, this);
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionFailed");

        stopLocationUpdates();
        stopSelf();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "GoogleApiClient connection has been suspend");
    }


    private class PostCoords extends AsyncTask<Void, Void, Void> {

        private Location l;

        PostCoords(Location location){
            this.l = location;
        }

        @Override
        protected Void doInBackground(Void... params) {
            //code to process location data.
            return null;
        }
    }
}

0 个答案:

没有答案