我正在使用谷歌地图v2。 我在一个自定义的地图片段中实现了OnMapReadyCallback,但是一旦片段开始它就不会触发onMapReady。
我的代码:
public class CustomMapFragment extends Fragment implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener{
OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_custom_map, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(this.getActivity());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(this);
return rootView;
}
覆盖方法
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.setMyLocationEnabled(true);
LatLng mMyLocation = new LatLng(mLat, mLng);
CameraPosition cameraPosition = new CameraPosition.Builder().target(mMyLocation).zoom(13).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Log.d("Map", "OnMapReady");
googleMap.setOnMarkerClickListener(this);
}
搜索但不运气。
答案 0 :(得分:0)
应用几项更改:
在onCreateView()
中移除mMapView.onResume();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_map, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
}
mMapView = (MapView) view.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
}
现在在onResume()
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
你的onMapReady()
看起来不错。
确保在xml中添加:
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />