当我第一次运行“地图”活动并接受位置权限时,如果我按“我的位置”按钮,则不会发生任何事情。
但是,如果我关闭活动并返回至该活动,此后该按钮将正常工作!因此,问题仅在我首次接受活动启动的权限之后。
如果我在接受权限后重新创建活动,问题就解决了,但是我不喜欢这种解决方案,而且我想了解问题的原因。
是什么导致此错误?
(NS)String
#
public class MapsActivity extends FragmentActivity implements` OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_NORMAL);
mapFragment.newInstance(options);
mapFragment.getMapAsync(this);
}
#
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
requestLocationPermissions();
mMap.getUiSettings().setMapToolbarEnabled(true);
}
public void requestLocationPermissions() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
15);
}else{
mMap.setMyLocationEnabled(true);//needs location permission
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}
答案 0 :(得分:0)
调试代码以检查首次授予权限时从纬度和经度中获得的值。此时可能未设置位置。如果尚未打开GPS,则获取精确位置有时会花费一些时间。我在用于测试的某些设备上遇到了同样的问题。
答案 1 :(得分:0)
我一直在努力解决这个问题,目前我发现的唯一解决方法是使用fuse location client来检索设备的位置并为“我的位置”按钮添加自定义行为。
这样,它可以一直工作,而无需重新加载活动。
private void getDeviceLocation() {
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
*/
try {
if (hasLocationPermission()) {
fusedLocationClient
.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
lastLocation = location;
// Logic to handle location object
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(),
location.getLongitude()), DEFAULT_ZOOM));
mMap.setOnMyLocationButtonClickListener(MapsActivity.this);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
MapsActivity是GoogleMap.OnMyLocationButtonClickListener的侦听器,然后有此代码
@Override
public boolean onMyLocationButtonClick() {
// I've tried just setting the default behavior of the button, but just works after reloading
// the activity, then I've managed the click event to latest location; now it works for
// all cases.
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lastLocation.getLatitude(),
lastLocation.getLongitude()), DEFAULT_ZOOM));
return false;
}
这是onMapReady的代码
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.getUiSettings().setZoomControlsEnabled(true);
getMuseums();
if (!hasLocationPermission()) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERMISSION_LOCATION);
return;
}
getDeviceLocation();
}
我的实际DEFAULT_ZOOM值为10。