Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Convert Android maps to GetMapAsync call (#824)
Browse files Browse the repository at this point in the history
* [A] Move map renderer to calling GetMapAsync

* [A] Make sure Map initializes correctly with Async map fetching
  • Loading branch information
Jason Smith authored and StephaneDelcroix committed Mar 21, 2017
1 parent 14a740d commit 8725484
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions Xamarin.Forms.Maps.Android/MapRenderer.cs
Expand Up @@ -16,7 +16,7 @@
namespace Xamarin.Forms.Maps.Android
{
public class MapRenderer : ViewRenderer<Map, MapView>,
GoogleMap.IOnCameraChangeListener
GoogleMap.IOnCameraChangeListener, IOnMapReadyCallback
{
const string MoveMessageName = "MapMoveToRegion";

Expand All @@ -35,9 +35,8 @@ public MapRenderer()

protected Map Map => Element;

#pragma warning disable 618
protected GoogleMap NativeMap => Control.Map;
#pragma warning restore 618
protected GoogleMap NativeMap;

internal static Bundle Bundle
{
set { s_bundle = value; }
Expand Down Expand Up @@ -81,7 +80,8 @@ protected override void Dispose(bool disposing)
NativeMap.SetOnCameraChangeListener(null);
NativeMap.InfoWindowClick -= MapOnMarkerClick;
NativeMap.Dispose();
}
NativeMap = null;
}

Control?.OnDestroy();
}
Expand All @@ -107,32 +107,17 @@ protected override void OnElementChanged(ElementChangedEventArgs<Map> e)

MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);

#pragma warning disable 618
if (oldMapView.Map != null)
if (NativeMap != null)
{
#pragma warning restore 618

#pragma warning disable 618
oldMapView.Map.SetOnCameraChangeListener(null);
#pragma warning restore 618
NativeMap.SetOnCameraChangeListener(null);
NativeMap.InfoWindowClick -= MapOnMarkerClick;
NativeMap = null;
}

oldMapView.Dispose();
}

GoogleMap map = NativeMap;
if (map != null)
{
map.SetOnCameraChangeListener(this);
NativeMap.InfoWindowClick += MapOnMarkerClick;

map.UiSettings.ZoomControlsEnabled = Map.HasZoomEnabled;
map.UiSettings.ZoomGesturesEnabled = Map.HasZoomEnabled;
map.UiSettings.ScrollGesturesEnabled = Map.HasScrollEnabled;
map.MyLocationEnabled = map.UiSettings.MyLocationButtonEnabled = Map.IsShowingUser;
SetMapType();
}
Control.GetMapAsync(this);

MessagingCenter.Subscribe<Map, MapSpan>(this, MoveMessageName, OnMoveToRegionMessage, Map);

Expand Down Expand Up @@ -180,13 +165,19 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)

if (_init)
{
MoveToRegion(Element.LastMoveToRegion, false);
OnCollectionChanged(Element.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
_init = false;
if (NativeMap != null)
{
MoveToRegion(Element.LastMoveToRegion, false);
OnCollectionChanged(Element.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
_init = false;
}
}
else if (changed)
{
UpdateVisibleRegion(NativeMap.CameraPosition.Target);
if (NativeMap != null)
{
UpdateVisibleRegion(NativeMap.CameraPosition.Target);
}
MoveToRegion(Element.LastMoveToRegion, false);
}
}
Expand Down Expand Up @@ -372,5 +363,23 @@ void UpdateVisibleRegion(LatLng pos)
double dlong = Math.Max(Math.Abs(ul.Longitude - lr.Longitude), Math.Abs(ur.Longitude - ll.Longitude));
Element.VisibleRegion = new MapSpan(new Position(pos.Latitude, pos.Longitude), dlat, dlong);
}

void IOnMapReadyCallback.OnMapReady(GoogleMap map)
{
NativeMap = map;
if (map == null)
{
return;
}

map.SetOnCameraChangeListener(this);
map.InfoWindowClick += MapOnMarkerClick;

map.UiSettings.ZoomControlsEnabled = Map.HasZoomEnabled;
map.UiSettings.ZoomGesturesEnabled = Map.HasZoomEnabled;
map.UiSettings.ScrollGesturesEnabled = Map.HasScrollEnabled;
map.MyLocationEnabled = map.UiSettings.MyLocationButtonEnabled = Map.IsShowingUser;
SetMapType();
}
}
}

0 comments on commit 8725484

Please sign in to comment.