【发布时间】:2017-12-14 15:14:45
【问题描述】:
我想使用 setMyLocationEnabled(true)。但由于权限问题无法使用。
我的 android 应用程序没有请求 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 的权限。未经许可,总是无法获得许可。
测试设备Android版本:6.0.1
编译 SDK 版本和目标 SDK:API-25,Android 7.1.1
构建工具版本:25.0.3
最低 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");
}
}
}
【问题讨论】:
-
您可能在请求权限对话框中选中了“不再询问”标志,尝试清除数据。而且您没有请求许可
-
安装“Google Play 服务”后就可以使用了。谢谢。
标签: android google-maps-android-api-2