Use .GetMapAsync instead .getMap method with Google Play Services (Xamarin)
Solution 1
Your map fragment class must implement OnMapReadyCallback
and override onMapReady()
:
@Override
public void onMapReady(final GoogleMap map) {
this.map = map;
map.setMyLocationEnabled(true);
}
In your onCreateView
use getMapAsync()
to set the callback on the fragment:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
All that you need to implement the google maps V2 is here: https://developers.google.com/maps/documentation/android/map
Solution 2
This might be late reply to your question, but may helpful to somebody else with the same issue. GetMapAsync() expects implementation of callback object of type IOnMapReadyCallback.
Find more detail here in my blog entry: http://appliedcodelog.com/2015/08/androidgmsmapsmapfragmentmap-is.html
//OnMapReadyClass
public class OnMapReadyClass :Java.Lang.Object,IOnMapReadyCallback
{
public GoogleMap Map { get; private set; }
public event Action<GoogleMap> MapReadyAction;
public void OnMapReady (GoogleMap googleMap)
{
Map = googleMap;
if ( MapReadyAction != null )
MapReadyAction (Map);
}
}
Now Call the GetMapAsync() and event Action return map instance on successful Map initialization.
//MyActivityClass.cs
GoogleMap map;
bool SetUpGoogleMap()
{
if(null != map ) return false;
var frag = FragmentManager.FindFragmentById<mapfragment>(Resource.Id.map);
var mapReadyCallback = new OnMapReadyClass();
mapReadyCallback.MapReadyAction += delegate(GoogleMap googleMap )
{
map=googleMap;
};
frag.GetMapAsync(mapReadyCallback);
return true;
}
Solution 3
Please see this:
public class MapActivity : Activity, IOnMapReadyCallback
{
public void OnMapReady(GoogleMap googleMap)
{
if (googleMap != null)
{
//Map is ready for use
}
}
}
Hope this will help you.
![Blasco73](https://i.stack.imgur.com/JbQVi.jpg?s=256&g=1)
Blasco73
We don't need to know the syntax!!! We know to write code!
Updated on July 05, 2022Comments
-
Blasco73 about 2 years
An old code works perfectly in this way:
LatLng location = new LatLng (myClass.myLocation.Latitude, myClass.myLocation.Longitude); CameraPosition.Builder builder = CameraPosition.InvokeBuilder (); builder.Target (location); builder.Zoom (18); CameraPosition cameraPosition = builder.Build (); MapsInitializer.Initialize (this); CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition (cameraPosition); MapFragment googleMap = FragmentManager.FindFragmentById<MapFragment> (Resource.Id.map); theMap = googleMap.Map; if (theMap != null) { theMap.MapType = GoogleMap.MapTypeNormal; theMap.MoveCamera (cameraUpdate); }
but now that the
.Map
is obsolete and deprecated, I must to use.GetMapAsync
in some way:theMap = googleMap.GetMapAsync (IOnMapReadyCallback);
But I don't understand how.
There is somebody that can help me?
-
Antonio Ribeiro almost 9 yearsI don't think you need to test for null Docs; At least is what I understood for it. But I don't know if the same can be said about Xamarin, I'm just a beginner and just stumbled in your answer and in the docs at the same time.
-
Daniel Krzyczkowski almost 9 yearsHello, please see here: developer.xamarin.com/guides/android/platform_features/… This is official Xamarin doc. You must check null.