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 called so that the map will not load.
The fragment implements OnMapReadyCallback
like needed and in the onCreateView
I get the MapView
and call getMapAsync
. If I place a breakpoint on that method, it will always been hit, the breakpoint inside onMapReady
never been hit. There is no exception thrown.
I've also a valid Google Maps API key in the file res/values/google_maps_api.xml
.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</RelativeLayout>
public class RoeteFragment extends Fragment implements OnMapReadyCallback {
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);
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();
}
}
Could you file the bug in my code?
Read
MapView
documentation. Especially:Try to use support map fragment. It will work in fragments also. https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment#developer-guide
For camera and zoom level check this -https://developers.google.com/maps/documentation/android-api/views#introduction
Try this and if it help accept it as answer.
You should notice this from the doc:
So, you are missing
mapView.onCreate(mapViewBundle)
in theonCreateView
, and since your map is in fully interactive mode, you must take care of the entire lifecylce, something like this:The map fragment is part of a layout you inflate to another fragment (let's call it parent). The inflated fragment becomes a child fragment NOT of the activity BUT of the parent fragment. You have to ask the parent fragment's child fragment manager:
Note: Just extend simple fragment
public class MapsFragments extends Fragment implements OnMapReadyCallback {}
XML fileand
Just let you know first this. I am using Google Map fragment as a child fragment inside an other fragment where GoogleMapFragment extents com.google.android.gms.maps.SupportMapFragment
I was using this piece of code in GoogleMapFragment in onCreateView, you can use in onStart as well. When onMapReady was not calling
I just removed this
and start calling like this
I hope this might help someone.
By a comment from @androidnoobdev where he said to use
SupportMapFragment
, I've update the code in my layout file to this:Update Removed MapView b'z not required after support fragment.
Thanks to @androidnoobdev