I\'ll try to show a map in my Android application on a fragment named RoeteFragment
. If I debug my code I see that the method onMapReady
is never c
You should notice this from the doc:
When using the API in fully interactive mode, users of the
MapView
class must forward all the activity life cycle methods to the corresponding methods in theMapView
class. Examples of the life cycle methods includeonCreate()
,onDestroy()
,onResume()
, andonPause()
.When using the
MapView
class in lite mode, forwarding lifecycle events is optional, except for the following situations:
It is mandatory to call
onCreate()
, otherwise no map will appear.If you wish to show the My Location dot on your lite mode map and use the default location source, you will need to call
onResume()
andonPause()
, because the location source will only update between these calls. If you use your own location source, it's not necessary to call these two methods.
So, you are missing mapView.onCreate(mapViewBundle)
in the onCreateView
, and since your map is in fully interactive mode, you must take care of the entire lifecylce, something like this:
public class RoeteFragment extends Fragment implements OnMapReadyCallback {
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
private MapView mapView;
private static Roete _roete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_roete, container, false);
mapView = (MapView) view.findViewById(R.id.map);
// *** IMPORTANT ***
// MapView requires that the Bundle you pass contain ONLY MapView SDK
// objects or sub-Bundles.
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
mapView.onCreate(mapViewBundle);
mapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (_roete != null && _roete.getAankomstLocatie() != null && _roete.getVertrekLocatie() != null) {
LatLng aankomst = new LatLng(_roete.getAankomstLocatie().getLatitude(), _roete.getAankomstLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(aankomst).title("aankomst"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));
LatLng vertrek = new LatLng(_roete.getVertrekLocatie().getLatitude(), _roete.getVertrekLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(vertrek).title("vertrek"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));
}
}
public static Fragment newInstance() {
return new RoeteFragment ();
}
public static Fragment newInstance(Roete roete) {
_roete = roete;
return newInstance();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null) {
mapViewBundle = new Bundle();
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mapView.onSaveInstanceState(mapViewBundle);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}