Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android xamarin google MapFragment.Map is null

in Xamarin android, creating MapFragment, the code shows map, but mapFragment.Map is always null and I can't set map type

code:

var mapFragment = MapFragment.NewInstance (mapOptions);
FragmentTransaction tx = FragmentManager.BeginTransaction();
tx.Add(Resource.Id.map_fragment_container, mapFragment);

map = mapFragment.Map;
if (map != null) {
    map.MapType = GoogleMap.MapTypeNormal;//never comes to this line
}
tx.Commit();

xml:

<FrameLayout
    android:id="@+id/map_fragment_container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
like image 454
Salome Tsiramua Avatar asked Mar 07 '26 17:03

Salome Tsiramua


1 Answers

the reason was that the map view was not created at the point of calling

    var mapFragment = MapFragment.NewInstance (mapOptions); 

so I used 500 milliseconds delay before accessing mapFragment.Map;

    handler.PostDelayed (UpdateMap, 500);


    void UpdateMap()
    {
        map = mapFragment.Map;
        if (map != null) {
            map.MapType = GoogleMap.MapTypeNormal;
            map.MoveCamera(cameraUpdate);
            return;
        }

        handler.PostDelayed(UpdateMap, 500);
    }
like image 88
Salome Tsiramua Avatar answered Mar 10 '26 06:03

Salome Tsiramua