Convert Android maps to GetMapAsync call (#824)

* [A] Move map renderer to calling GetMapAsync

* [A] Make sure Map initializes correctly with Async map fetching
This commit is contained in:
Jason Smith 2017-03-21 03:38:36 -07:00 коммит произвёл Stephane Delcroix
Родитель 14a740dd5e
Коммит 8725484ab0
1 изменённых файлов: 37 добавлений и 28 удалений

Просмотреть файл

@ -16,7 +16,7 @@ using Math = System.Math;
namespace Xamarin.Forms.Maps.Android namespace Xamarin.Forms.Maps.Android
{ {
public class MapRenderer : ViewRenderer<Map, MapView>, public class MapRenderer : ViewRenderer<Map, MapView>,
GoogleMap.IOnCameraChangeListener GoogleMap.IOnCameraChangeListener, IOnMapReadyCallback
{ {
const string MoveMessageName = "MapMoveToRegion"; const string MoveMessageName = "MapMoveToRegion";
@ -35,9 +35,8 @@ namespace Xamarin.Forms.Maps.Android
protected Map Map => Element; protected Map Map => Element;
#pragma warning disable 618 protected GoogleMap NativeMap;
protected GoogleMap NativeMap => Control.Map;
#pragma warning restore 618
internal static Bundle Bundle internal static Bundle Bundle
{ {
set { s_bundle = value; } set { s_bundle = value; }
@ -81,7 +80,8 @@ namespace Xamarin.Forms.Maps.Android
NativeMap.SetOnCameraChangeListener(null); NativeMap.SetOnCameraChangeListener(null);
NativeMap.InfoWindowClick -= MapOnMarkerClick; NativeMap.InfoWindowClick -= MapOnMarkerClick;
NativeMap.Dispose(); NativeMap.Dispose();
} NativeMap = null;
}
Control?.OnDestroy(); Control?.OnDestroy();
} }
@ -107,32 +107,17 @@ namespace Xamarin.Forms.Maps.Android
MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName); MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
#pragma warning disable 618 if (NativeMap != null)
if (oldMapView.Map != null)
{ {
#pragma warning restore 618 NativeMap.SetOnCameraChangeListener(null);
#pragma warning disable 618
oldMapView.Map.SetOnCameraChangeListener(null);
#pragma warning restore 618
NativeMap.InfoWindowClick -= MapOnMarkerClick; NativeMap.InfoWindowClick -= MapOnMarkerClick;
NativeMap = null;
} }
oldMapView.Dispose(); oldMapView.Dispose();
} }
GoogleMap map = NativeMap; Control.GetMapAsync(this);
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();
}
MessagingCenter.Subscribe<Map, MapSpan>(this, MoveMessageName, OnMoveToRegionMessage, Map); MessagingCenter.Subscribe<Map, MapSpan>(this, MoveMessageName, OnMoveToRegionMessage, Map);
@ -180,13 +165,19 @@ namespace Xamarin.Forms.Maps.Android
if (_init) if (_init)
{ {
MoveToRegion(Element.LastMoveToRegion, false); if (NativeMap != null)
OnCollectionChanged(Element.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); {
_init = false; MoveToRegion(Element.LastMoveToRegion, false);
OnCollectionChanged(Element.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
_init = false;
}
} }
else if (changed) else if (changed)
{ {
UpdateVisibleRegion(NativeMap.CameraPosition.Target); if (NativeMap != null)
{
UpdateVisibleRegion(NativeMap.CameraPosition.Target);
}
MoveToRegion(Element.LastMoveToRegion, false); MoveToRegion(Element.LastMoveToRegion, false);
} }
} }
@ -372,5 +363,23 @@ namespace Xamarin.Forms.Maps.Android
double dlong = Math.Max(Math.Abs(ul.Longitude - lr.Longitude), Math.Abs(ur.Longitude - ll.Longitude)); 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); 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();
}
} }
} }