我想使用setMyLocationEnabled(true)。但由于许可问题而无法使用它。
我的Android应用程序并未要求获得ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION的许可。如果没有获得许可,它总是无法获得许可。
Android版测试设备:6.0.1
编译SDK版本和目标SDK:API-25,Android 7.1.1
构建工具版本:25.0.3
Min SDK版本:API-22,Android 5.1
AndroidManifest.xml中添加了以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES"/>
<uses-permission android:name="edu.bloomu.huskies.tsc71523.skatelogger.MAPS_RECEIVE"/>
MapsActivity.java:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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;
LatLng sydney = new LatLng(0, 0);
mMap.addMarker(new MarkerOptions().position(sydney).title("TestLocation"));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.e("LOCATION", "ACCESS_FINE_LOCATION-------Successful");
mMap.setMyLocationEnabled(true);
}
else {
Log.e("LOCATION", "ACCESS_FINE_LOCATION-------Failed");
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.e("LOCATION", "ACCESS_COARSE_LOCATION-------Successful");
mMap.setMyLocationEnabled(true);
}
else{
Log.e("LOCATION", "ACCESS_COARSE_LOCATION-------Failed");
}
}
}
答案 0 :(得分:1)
从Android Marshmallow(6.0)开始,如果您的目标是API级别23或更高,则必须使用新的权限模型(请求权限)。
本演讲将帮助您了解新模型:https://youtu.be/WGz-alwVh8A
此帮助程序库还可以更轻松地使用和无缝支持旧版Android:https://github.com/hiqes/andele