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" />
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With