这是我在片段中的代码
@Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setTrafficEnabled(false);
map.setIndoorEnabled(false);
map.setBuildingsEnabled(false);
map.getUiSettings().setZoomControlsEnabled(true);
map.setMyLocationEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
LatLng wotw = new LatLng(26.912434, 75.787270);
map.addMarker(new MarkerOptions().position(wotw)
.title("Walk of the Week"));
map.moveCamera(CameraUpdateFactory.newLatLng(wotw));
map.animateCamera(CameraUpdateFactory.zoomTo(15.0f));
}
我已经尝试了所有权限,并且我已经添加了manifest
也得到了错误
my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
请提前解决这个问题谢谢你,我将不胜感激每一个建议
在设置map.setMyLocationEnabled(true);
之前,您必须声明并检查位置权限:
首先,您需要在AndroidManifest.xml
中声明位置权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
之后,您需要在设置map.setMyLocationEnabled(true);
之前检查授予的权限:
String MY_LOCATION_REQUEST_CODE= "LOCATION_PERMISSIONS";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_LOCATION_REQUEST_CODE);
}
}
下一步,您需要检查授予的此权限:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
更多信息,请参阅。 官方文档