我正在尝试构建始终跟踪我的客户位置的应用程序,该应用程序始终显示该位置为空。在 Manifest 文件中,我拥有 (ACCESS_FINE_LOCATION) 和 (ACCESS_COARSE_LOCATION) 权限。
问题是每次我打开应用程序时,它都会显示 Toast 消息( Location 为 null ),正如我在代码中所述,但我不知道获取 null 位置的原因是什么,即使我正在使用 (requestLocationUpdates ) (FusedLocationProviderClient) 类的方法..
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.tasks.OnSuccessListener;
public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback {
// constants
private static final String TAG = "DriverMapActivity";
private static final int MY_REQUEST_LOCATION_CODE = 101;
// vars
private GoogleMap mMap;
// private boolean mLocationPermissionGranted = false;
private FusedLocationProviderClient mFusedLocationProviderClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
private LatLng newLatLng;
// vars
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_map);
// start receiving location updates
getMyLocation();
// 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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
private void getMyLocation(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// permission is granted
// and we can now use getLastLocation method
mFusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(newLatLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
Log.d(TAG, "onSuccess: Latitude = " + location.getLatitude());
Log.d(TAG, "onSuccess: Longitude = " + location.getLongitude());
} else {
Toast.makeText(DriverMapActivity.this, "Location is null", Toast.LENGTH_SHORT).show();
}
}
});
} else {
// permission is not granted.
// we have to request the user to grant the the ACCESS_FINE_LOCATION permission.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_REQUEST_LOCATION_CODE);
}
}
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for (Location location : locationResult.getLocations()) {
if (location != null) {
newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(newLatLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
Log.d(TAG, "onSuccess: Latitude = " + location.getLatitude());
Log.d(TAG, "onSuccess: Longitude = " + location.getLongitude());
} else {
Toast.makeText(DriverMapActivity.this, "Location is null", Toast.LENGTH_SHORT).show();
}
}
}
};
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_REQUEST_LOCATION_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission has been granted and we can proceed with app
} else {
Toast.makeText(this, "app closed because you didn't granted the required permission", Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
}else{
// permission is not granted.
// we have to request the user to grant the the ACCESS_FINE_LOCATION permission.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_REQUEST_LOCATION_CODE);
}
}
}
private void stopLocationUpdates() {
mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
}
@Override
protected void onResume() {
super.onResume();
startLocationUpdates();
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
@Override
protected void onStop() {
super.onStop();
stopLocationUpdates();
}
}