融合位置始终返回null

时间:2017-02-05 16:55:45

标签: android geolocation android-fusedlocation

我正在使用Fused Location来获取我的设备的当前位置,但是我实现的代码总是返回NULL。我已经仔细检查过我的设备位置是否开启并设置为高精度模式。请告诉我的代码中有什么问题?

我的代码:

import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
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;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;



public class FusedLocationTest implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static Location mLastLocation;
    private static GoogleApiClient mGoogleApiClient;
    private static Context context;


    public Location getCurrentLocation(Context context) {
        this.context = context;
        buildGoogleApiClient();
        mGoogleApiClient.connect();
        if ((ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        return mLastLocation;
    }


    /*connect to fused location provider*/
    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }


    @Override
    public void onConnected(Bundle bundle) {

    }


    @Override
    public void onLocationChanged(Location location) {

    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        buildGoogleApiClient();
    }


    @Override
    public void onConnectionSuspended(int i) {

    }

}

致电按钮点击:

@Override
    public void onClick(View v) {
        Location l = new FusedLocationTest().getCurrentLocation(this);
        if(l!=null)
            Toast.makeText(this,l.getLatitude()+", "+l.getLongitude(), Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this,"NULL LOCATION", Toast.LENGTH_LONG).show();
    }

2 个答案:

答案 0 :(得分:1)

mGoogleApiClient.connect()函数是异步的。您必须等到onConnected,直到最后一个位置可用。 documentation中有一个明显的例子。

答案 1 :(得分:1)

将此代码添加到您的onConnected,即它将获得Last Known Location。

private static Location mLastLocation;
private static GoogleApiClient mGoogleApiClient;
private static Context context;

// added
private LocationRequest mLocationRequest; 
private Double latitude;
private Double longitude;
private String TAG = ""; // set your TAG

@Override
public void onConnected(Bundle bundle) {

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startLocationUpdates();
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if(mLastLocation == null){
        startLocationUpdates();
    }

    if (mLastLocation != null) {
        latitude = mLocation.getLatitude();
        longitude = mLocation.getLongitude();

        // set your tag
        Log.d(TAG, String.valueOf(latitude));
        Log.d(TAG, String.valueOf(longitude));

    } else {
        Toast.makeText(context, "Location not Detected, Did you turn off your location?", Toast.LENGTH_SHORT).show();
    }
}

protected void startLocationUpdates() {
    // Create the location request
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(30 * 1000)
            .setFastestInterval(5 * 1000);

    // Request location updates
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

从您的方法中删除synchronized,只需将其公开。如下所示:

public void buildGoogleApiClient() { }