Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapView.onMapReady never called in Fragment for load Google Map in MapView

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?

like image 720
H. Pauwelyn Avatar asked Apr 23 '16 09:04

H. Pauwelyn


2 Answers

Read MapView documentation. Especially:

Users of this class must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

  • onCreate(Bundle)
  • onResume()
  • onPause()
  • onDestroy()
  • onSaveInstanceState()
  • onLowMemory()
like image 89
Eugen Pechanec Avatar answered Oct 08 '22 08:10

Eugen Pechanec


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 file

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.elektrasoft.Test.MapsFragments" />

and

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    Log.d("check","onCreateView");
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_maps_fragments, container, false);
    // Gets the MapView from the XML layout and creates it
    final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
    myMAPF.getMapAsync(this);
    return  view;
}`
like image 7
Shahbaz Mancho Avatar answered Oct 08 '22 08:10

Shahbaz Mancho