问:=>我在布局中使用以下MapView:-
-------------------------------------------------- ----------
<com.google.android.gms.maps.MapView
android:id =“ @ + id / map”
class =“ com.google.android.gms.maps.MapFragment”
android:layout_width =“ match_parent”
android:layout_height =“ 190dp”
android:layout_gravity =“ bottom”
android:layout_marginTop =“ 5dp”
android:layout_marginBottom =“ 10dp”
android:layout_marginLeft =“ 10dp”
android:layout_marginRight =“ 10dp”
android:layout_below =“ @ + id / ivLocation”
android:padding =“ 1dp”
android:background =“#D3D3D3”
map:liteMode =“ true”
/>
=>我在片段的onCreateView()中有以下代码:-
-------------------------------------------------- -------------------------------------
MapsInitializer.initialize(this.getActivity());
mapView =(MapView)view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
mapView.setClickable(false);
=>而且我在片段类中也有以下代码:-
-------------------------------------------------- --------------------------------------
@Override
公共无效onPause(){
super.onPause();
mapView.onPause();
}
@Override
公共无效onDestroy(){
super.onDestroy();
mapView.onDestroy();
}
@Override
公共无效onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
公共无效onLowMemory(){
super.onLowMemory();
mapView.onLowMemory();
}
@Override
公共无效onResume(){
super.onResume();
mapView.onResume();
}
@Override
公共无效onDestroyView(){
super.onDestroyView();
}
@Override
public void onMapReady(GoogleMap googleMap){
map = googleMap;
如果(ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.ACCESS_COARSE_LOCATION){_PackageManager.PERMISSION
// TODO:考虑调用
// ActivityCompat#requestPermissions
//这里请求缺少的权限,然后覆盖
// public void onRequestPermissionsResult(int requestCode,String []权限,
// int [] grantResults)
//处理用户授予权限的情况。请参阅说明文件
//有关ActivityCompat#requestPermissions的更多信息。
返回;
}
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager locationManager =(LocationManager)getContext()。getSystemService(Context.LOCATION_SERVICE);
位置lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng latLng = new LatLng(lastLocation.getLatitude(),lastLocation.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
map.addMarker(new MarkerOptions()。position(latLng).title(“登录时的城市协调器位置”).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
map.getUiSettings()。setMapToolbarEnabled(false);
}
此代码在装有棒棒糖android的moto e2手机中工作,但在装有棉花糖android的moto g3手机中不工作。在棉花糖中,在googlemap精简模式下根本不会加载地图。如果我在mapview中删除此属性(map:liteMode =“ true”),则表示地图正在加载,但标记不存在。
答:在手机设置中为该应用启用位置权限后,此功能便可以正常使用。