Reputation: 21
I'm working on google map v2. I implement OnMapReadyCallback in a custom map fragment but it's not trigger onMapReady once fragment start.
my code:
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 method
@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);
}
Search around but not luck.
Upvotes: 1
Views: 1539
Reputation: 23881
Apply couple of changes:
in onCreateView()
remove 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);
}
Now in onResume()
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
your onMapReady()
looks ok.
Make sure you add this in xml:
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 1