Android google map api getlastlocation()始终返回null

时间:2017-05-06 13:23:51

标签: java android google-maps google-api

我确实需要持续获取用户位置;因此,我使用getlastlocation()来获取用户的当前位置。但是,它始终返回null。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks
    ,GoogleApiClient.OnConnectionFailedListener{

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    protected double x, y;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    if(mGoogleApiClient == null){
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
}

protected void onStart(){
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {

    if(mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

@Override
public void onConnected(Bundle bundle){
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }
    else {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation != null){
            x = mLastLocation.getLatitude();
            y = mLastLocation.getLongitude();
        }
        else{
            Log.e("error", "null");
        }
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng gpsLocation = new LatLng(x, y);
    mMap.addMarker(new MarkerOptions().position(gpsLocation).title("Here"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(gpsLocation));
}
始终执行

Log.e("error", "null"),这意味着getlastlocation()始终返回null。在AndroidManifest中,我有足够的权限

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

我在stackOverflow上搜索了类似的帖子,他们给出了一些理由(例如:GPS没有打开),但它们都不适用于我的情况。

1 个答案:

答案 0 :(得分:0)

您是否使用LocationRequest创建了requestLocationUpdates并要求进行位置更新?

您需要像下面的代码一样创建LocationRequest

LocationRequest mLocationRequest;

protected void createLocationRequest() {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

}

使用LocationRequest,您需要像下面的代码一样调用requestLocationUpdates FusedLocationApi

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

之后,您将获得getlastlocation() FusedLocationApi方法的位置。

如果不需要,请致电removeLocationUpdates

 protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
}