Skip to content

Instantly share code, notes, and snippets.

@MilenPavlov
Created April 9, 2015 08:08
Show Gist options
  • Save MilenPavlov/84fb89e04f735c90874c to your computer and use it in GitHub Desktop.
Save MilenPavlov/84fb89e04f735c90874c to your computer and use it in GitHub Desktop.
Using new async Google Maps in Xamarin Android
//_mapFragment.Map is now obsolete with latest Google Play services.. new approach is needed...
private void SetUpMap()
{
_mapFragment = FragmentManager.FindFragmentByTag("map") as MapFragment;
if (_mapFragment == null)
{
GoogleMapOptions mapOptions = new GoogleMapOptions()
.InvokeMapType(GoogleMap.MapTypeHybrid)
.InvokeZoomControlsEnabled(true)
.InvokeCompassEnabled(true);
FragmentTransaction fragTx = FragmentManager.BeginTransaction();
_mapFragment = MapFragment.NewInstance(mapOptions);
fragTx.Add(Resource.Id.map, _mapFragment, "map");
fragTx.Commit();
var mapReadyCallback = new MyOnMapReady();
mapReadyCallback.MapReady += (sender, args) =>
{
_gettingMap = false;
_map = ((MyOnMapReady)sender).Map;//receive the Map object when completed
MoveCamera();//do all the usual stuff after
};
_gettingMap = true;
_mapFragment.GetMapAsync(mapReadyCallback);
}
}
//class that implements IOnMapReadyCallback
public class MyOnMapReady : Java.Lang.Object, IOnMapReadyCallback
{
public GoogleMap Map { get; private set; }
public event EventHandler MapReady;
public void OnMapReady(GoogleMap googleMap)
{
Map = googleMap;
var handler = MapReady;
if (handler != null)
handler(this, EventArgs.Empty);//send the Map object back when completed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment