我正在使用地图视图显示地图,在API 23之前,我的应用程序工作正常且显示当前位置,但当我在23及以上运行时,它没有显示当前位置。我的代码如下:
mMap = googleMap;
mMap.getUiSettings().setMapToolbarEnabled(false);
LatLng loc = new LatLng(currentLatitude, currentLongitude);
//mMap.addMarker(new MarkerOptions().position(loc).title(currentLocation + ", none"));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 14.0f));
if (ActivityCompat.checkSelfPermission(MapClass.this.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapClass.this.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
帮帮我?
答案 0 :(得分:0)
检查有关请求Permissions at Run Time的文件,说明:
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。此方法简化了应用安装过程,因为用户在安装或更新应用时无需授予权限。它还使用户可以更好地控制应用程序的功能;例如,用户可以选择让相机应用程序访问相机,但不能访问设备位置。用户可以通过转到应用程序的“设置”屏幕随时撤消权限。
如果设备运行的是Android 6.0或更高版本,并且您的应用的目标SDK为23或更高:该应用有列出清单中的权限,它必须请求每个危险应用程序运行时需要的权限。用户可以授予或拒绝每个权限,即使用户拒绝权限请求,应用也可以继续以有限的功能运行
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return;
}
if (PermissionUtils.isPermissionGranted(permissions, grantResults,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Enable the my location layer if the permission has been granted.
enableMyLocation();
} else {
// Display the missing permission error dialog when the fragments resume.
mPermissionDenied = true;
}
}
GitHub上的ApiDemos repository包含演示在地图上使用位置的示例:
希望这有帮助。
答案 1 :(得分:0)
我可能会有点迟到,但这个答案可以帮助那些遇到同样问题且希望在两个版本上运行应用程序的人。问题是,对于超过21的API,你需要要求Run time permission。
首先,我创建了一个名为gettingLocationBasedOnApiVersion(mMap)
的函数,其中我放置了我使用用户位置的所有代码。但事先在onMapReady()
我检查了API版本,如果API版本超过21,我就要求权限。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(Build.VERSION.SDK_INT <= 21){
gettingLocationBasedOnApiVersion(mMap);
Toast.makeText(getApplicationContext(), "APK less than 21", Toast.LENGTH_LONG).show();
}else{
Log.i("SDK Version", "SDK more than 21 will require permissions on run time");
Toast.makeText(getApplicationContext(), "APK greater than 21", Toast.LENGTH_LONG).show();
enableMyLocation();
}
}
然后在启用功能中我询问了运行时权限。
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private boolean mPermissionDenied = false;
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission to access the location is missing.
PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (mMap != null) {
// Access to the location has been granted to the app.
mMap.setMyLocationEnabled(true);
gettingLocationBasedOnApiVersion(mMap);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return;
}
if (PermissionUtils.isPermissionGranted(permissions, grantResults,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Enable the my location layer if the permission has been granted.
enableMyLocation();
} else {
// Display the missing permission error dialog when the fragments resume.
mPermissionDenied = true;
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if (mPermissionDenied) {
// Permission was not granted, display error dialog.
showMissingPermissionError();
mPermissionDenied = false;
}
}
/**
* Displays a dialog with error message explaining that the location permission is missing.
*/
private void showMissingPermissionError() {
PermissionUtils.PermissionDeniedDialog
.newInstance(true).show(getSupportFragmentManager(), "dialog");
}
为了更好地理解,您可以在我的github页面上参考Places Class。
PS。不要忘记在app
文件夹中添加PermissionUtils类。