Created
April 9, 2015 08:08
-
-
Save MilenPavlov/84fb89e04f735c90874c to your computer and use it in GitHub Desktop.
Using new async Google Maps in Xamarin Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//_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