我需要使用setMyLocation和标记在Fragment
中使用Google地图,但在android 6中我只看到地图,没有我的位置按钮和标记。在Android 4,5中,一切看起来都不错。
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.team.noty.carowner.fragment.Map">
<Spinner
android:id="@+id/tech_centre_spinner_map"
android:entries="@array/Услуги"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/tech_centre_spinner_mark_map"
android:entries="@array/Марки"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tech_centre_spinner_map"
/>
<com.google.android.gms.maps.MapView
android:layout_marginTop="5dp"
android:layout_below="@+id/tech_centre_spinner_map"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container,
false);
mMapView = (MapView) view.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
service = (Spinner)
view.findViewById(R.id.tech_centre_spinner_map);
mark = (Spinner)
view.findViewById(R.id.tech_centre_spinner_mark_map);
mMapView.onResume(); // needed to get the map to display
immediately
typeSpinner();
speckSpinner();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
if (ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
LatLng sydney = new LatLng(-49.1222, 34.2322);
googleMap.addMarker(new
MarkerOptions().position(sydney).title("Marker Title").snippet("Marker
Description"));
// For zooming automatically to the location of the
marker
CameraPosition cameraPosition = new
CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosit ion));
}
});
return view;
}
答案 0 :(得分:0)
如果有人需要,我找到了ansver:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
//*** Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER)) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(55.751244, 37.618423))
.zoom(12)
.bearing(0)
.tilt(20)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
}else{
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(55.751244, 37.618423))
.zoom(13)
.bearing(0)
.tilt(20)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
}
}
});
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
},
MY_LOCATION_REQUEST_CODE);
}