我使用熔融定位提供程序客户端获取设备位置。但是如果位置设置关闭了我的手机,并使用locationServices打开了位置设置。 getSettingsClient融合的位置提供者返回null
final Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
locationTask.addOnSuccessListener((Activity) context, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
lastKnownLocation = location;
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude()), Constants.DEFAULT_ZOOM));
LatLng position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
HashMap<String, String> adressInfo = MapUtility.getAddressGeocoderObject(context, position);
MapUtility.moveCamera(context, map, position, Constants.DEFAULT_ZOOM,
adressInfo.get("addressFeatureName"), adressInfo.get("remainAdress"));
} else {
Toast.makeText(context, "null", Toast.LENGTH_LONG).show();
//map.getUiSettings().setMyLocationButtonEnabled(false);
}
答案 0 :(得分:0)
解决了我的问题,当更新融合位置提供程序
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10*1000);
mLocationRequest.setFastestInterval(10*1000);
mLocationRequest.setSmallestDisplacement(10);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void createLocationCallback(){
locationCallback=new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
//code here
}
};
}
如果位置为空则更新融合的位置提供者
public void getDeviceLocation() {
try {
final Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
locationTask.addOnSuccessListener((Activity) context, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
lastKnownLocation = location;
if(map==null){
Toast.makeText(context,"map null",Toast.LENGTH_LONG).show();
return;
}
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude()), Constants.DEFAULT_ZOOM));
LatLng position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
HashMap<String, String> adressInfo = MapUtility.getAddressGeocoderObject(context, position);
assert adressInfo != null;
MapUtility.moveCamera(context, map, position, Constants.DEFAULT_ZOOM,
adressInfo.get("addressFeatureName"), adressInfo.get("remainAdress"));
} else {
createLocationRequest();
createLocationCallback();
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest,locationCallback,Looper.myLooper());
}
}
});
locationTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(Constants.PlacesTag, "Current location is null. Using defaults.");
}
});
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}