summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.Android
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-09-16 18:55:38 (GMT)
committerRui Marinho <me@ruimarinho.net>2016-09-16 18:55:38 (GMT)
commit5b1bad9a1137b4ec224a937ab7f8405929014e65 (patch)
tree8375915ef714196c9fb4e95cedf64029f99e2c67 /Xamarin.Forms.Maps.Android
parentd3cf333590a1c762e04e3083003076e1d23cdcf5 (diff)
downloadxamarin-forms-5b1bad9a1137b4ec224a937ab7f8405929014e65.zip
xamarin-forms-5b1bad9a1137b4ec224a937ab7f8405929014e65.tar.gz
xamarin-forms-5b1bad9a1137b4ec224a937ab7f8405929014e65.tar.bz2
Format MapRenderer according to project code style (#362)
* Format MapRenderer according to project code style * Using var instead of explicit type for Markers
Diffstat (limited to 'Xamarin.Forms.Maps.Android')
-rw-r--r--Xamarin.Forms.Maps.Android/MapRenderer.cs438
1 files changed, 243 insertions, 195 deletions
diff --git a/Xamarin.Forms.Maps.Android/MapRenderer.cs b/Xamarin.Forms.Maps.Android/MapRenderer.cs
index 29270c0..dc1b46c 100644
--- a/Xamarin.Forms.Maps.Android/MapRenderer.cs
+++ b/Xamarin.Forms.Maps.Android/MapRenderer.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
@@ -10,35 +11,45 @@ using Android.OS;
using Java.Lang;
using Xamarin.Forms.Platform.Android;
using Math = System.Math;
-using Android.Runtime;
-using System.Collections;
namespace Xamarin.Forms.Maps.Android
{
- public class MapRenderer : ViewRenderer<Map,MapView>,
- GoogleMap.IOnCameraChangeListener
+ public class MapRenderer : ViewRenderer<Map, MapView>,
+ GoogleMap.IOnCameraChangeListener
{
- public MapRenderer ()
- {
- AutoPackage = false;
- }
+ const string MoveMessageName = "MapMoveToRegion";
static Bundle s_bundle;
- internal static Bundle Bundle { set { s_bundle = value; } }
+
+ bool _disposed;
+
+ bool _init = true;
List<Marker> _markers;
- const string MoveMessageName = "MapMoveToRegion";
+ public MapRenderer()
+ {
+ AutoPackage = false;
+ }
+
+ protected Map Map => Element;
#pragma warning disable 618
- protected GoogleMap NativeMap => ((MapView) Control).Map;
+ protected GoogleMap NativeMap => Control.Map;
#pragma warning restore 618
+ internal static Bundle Bundle
+ {
+ set { s_bundle = value; }
+ }
- protected Map Map => (Map) Element;
+ public void OnCameraChange(CameraPosition pos)
+ {
+ UpdateVisibleRegion(pos.Target);
+ }
- public override SizeRequest GetDesiredSize (int widthConstraint, int heightConstraint)
+ public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
{
- return new SizeRequest (new Size (Context.ToPixels (40), Context.ToPixels (40)));
+ return new SizeRequest(new Size(Context.ToPixels(40), Context.ToPixels(40)));
}
protected override MapView CreateNativeControl()
@@ -46,29 +57,57 @@ namespace Xamarin.Forms.Maps.Android
return new MapView(Context);
}
- protected override void OnElementChanged (ElementChangedEventArgs<Map> e)
+ protected override void Dispose(bool disposing)
{
- base.OnElementChanged (e);
+ if (disposing && !_disposed)
+ {
+ _disposed = true;
- var oldMapView = (MapView)Control;
+ Map mapModel = Element;
+ if (mapModel != null)
+ {
+ MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
+ ((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;
+ }
- var mapView = CreateNativeControl();
- mapView.OnCreate (s_bundle);
- mapView.OnResume ();
- SetNativeControl (mapView);
+ GoogleMap gmap = NativeMap;
+ if (gmap == null)
+ {
+ return;
+ }
+ gmap.MyLocationEnabled = false;
+ gmap.InfoWindowClick -= MapOnMarkerClick;
+ gmap.Dispose();
+ }
+
+ base.Dispose(disposing);
+ }
+
+ protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
+ {
+ base.OnElementChanged(e);
+
+ MapView oldMapView = Control;
- if (e.OldElement != null) {
- var oldMapModel = e.OldElement;
+ MapView mapView = CreateNativeControl();
+ mapView.OnCreate(s_bundle);
+ mapView.OnResume();
+ SetNativeControl(mapView);
+
+ if (e.OldElement != null)
+ {
+ Map oldMapModel = e.OldElement;
((ObservableCollection<Pin>)oldMapModel.Pins).CollectionChanged -= OnCollectionChanged;
- MessagingCenter.Unsubscribe<Map, MapSpan> (this, MoveMessageName);
+ MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
#pragma warning disable 618
- if (oldMapView.Map != null) {
+ if (oldMapView.Map != null)
+ {
#pragma warning restore 618
#pragma warning disable 618
- oldMapView.Map.SetOnCameraChangeListener (null);
+ oldMapView.Map.SetOnCameraChangeListener(null);
#pragma warning restore 618
NativeMap.InfoWindowClick -= MapOnMarkerClick;
}
@@ -76,179 +115,96 @@ namespace Xamarin.Forms.Maps.Android
oldMapView.Dispose();
}
- var map = NativeMap;
- if (map != null) {
- map.SetOnCameraChangeListener (this);
+ 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 ();
+ SetMapType();
}
-
- MessagingCenter.Subscribe<Map, MapSpan> (this, MoveMessageName, OnMoveToRegionMessage, Map);
+
+ MessagingCenter.Subscribe<Map, MapSpan>(this, MoveMessageName, OnMoveToRegionMessage, Map);
var incc = Map.Pins as INotifyCollectionChanged;
if (incc != null)
+ {
incc.CollectionChanged += OnCollectionChanged;
- }
-
- void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
- {
- switch (notifyCollectionChangedEventArgs.Action) {
- case NotifyCollectionChangedAction.Add:
- AddPins (notifyCollectionChangedEventArgs.NewItems);
- break;
- case NotifyCollectionChangedAction.Remove:
- RemovePins (notifyCollectionChangedEventArgs.OldItems);
- break;
- case NotifyCollectionChangedAction.Replace:
- RemovePins (notifyCollectionChangedEventArgs.OldItems);
- AddPins (notifyCollectionChangedEventArgs.NewItems);
- break;
- case NotifyCollectionChangedAction.Reset:
- _markers?.ForEach (m => m.Remove ());
- _markers = null;
- AddPins ((IList)(Element as Map).Pins);
- break;
- case NotifyCollectionChangedAction.Move:
- //do nothing
- break;
- }
- }
-
- void OnMoveToRegionMessage (Map s, MapSpan a)
- {
- MoveToRegion (a, true);
- }
-
- void MoveToRegion (MapSpan span, bool animate)
- {
- var map = NativeMap;
- if (map == null)
- return;
-
- span = span.ClampLatitude (85, -85);
- var ne = new LatLng (span.Center.Latitude + span.LatitudeDegrees / 2, span.Center.Longitude + span.LongitudeDegrees / 2);
- var sw = new LatLng (span.Center.Latitude - span.LatitudeDegrees / 2, span.Center.Longitude - span.LongitudeDegrees / 2);
- var update = CameraUpdateFactory.NewLatLngBounds (new LatLngBounds (sw, ne), 0);
-
- try {
- if (animate)
- map.AnimateCamera (update);
- else
- map.MoveCamera (update);
- } catch (IllegalStateException exc) {
- System.Diagnostics.Debug.WriteLine ("MoveToRegion exception: " + exc);
- }
- }
-
- bool _init = true;
-
- protected override void OnLayout (bool changed, int l, int t, int r, int b)
- {
- base.OnLayout (changed, l, t, r, b);
-
- if (_init) {
- MoveToRegion (((Map)Element).LastMoveToRegion, false);
- OnCollectionChanged (((Map)Element).Pins, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Reset));
- _init = false;
- } else if (changed) {
- UpdateVisibleRegion (NativeMap.CameraPosition.Target);
}
}
- protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
+ protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
- base.OnElementPropertyChanged (sender, e);
+ base.OnElementPropertyChanged(sender, e);
- if (e.PropertyName == Map.MapTypeProperty.PropertyName) {
+ if (e.PropertyName == Map.MapTypeProperty.PropertyName)
+ {
SetMapType();
return;
}
-
- var gmap = NativeMap;
+
+ GoogleMap gmap = NativeMap;
if (gmap == null)
+ {
return;
+ }
if (e.PropertyName == Map.IsShowingUserProperty.PropertyName)
+ {
gmap.MyLocationEnabled = gmap.UiSettings.MyLocationButtonEnabled = Map.IsShowingUser;
+ }
else if (e.PropertyName == Map.HasScrollEnabledProperty.PropertyName)
+ {
gmap.UiSettings.ScrollGesturesEnabled = Map.HasScrollEnabled;
- else if (e.PropertyName == Map.HasZoomEnabledProperty.PropertyName) {
+ }
+ else if (e.PropertyName == Map.HasZoomEnabledProperty.PropertyName)
+ {
gmap.UiSettings.ZoomControlsEnabled = Map.HasZoomEnabled;
gmap.UiSettings.ZoomGesturesEnabled = Map.HasZoomEnabled;
}
}
- void SetMapType ()
+ protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
- var map = NativeMap;
- if (map == null)
- return;
+ base.OnLayout(changed, l, t, r, b);
- switch (Map.MapType) {
- case MapType.Street:
- map.MapType = GoogleMap.MapTypeNormal;
- break;
- case MapType.Satellite:
- map.MapType = GoogleMap.MapTypeSatellite;
- break;
- case MapType.Hybrid:
- map.MapType = GoogleMap.MapTypeHybrid;
- break;
- default:
- throw new ArgumentOutOfRangeException ();
+ if (_init)
+ {
+ MoveToRegion(Element.LastMoveToRegion, false);
+ OnCollectionChanged(Element.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
+ _init = false;
+ }
+ else if (changed)
+ {
+ UpdateVisibleRegion(NativeMap.CameraPosition.Target);
}
}
- public void OnCameraChange (CameraPosition pos)
- {
- UpdateVisibleRegion (pos.Target);
- }
-
- void UpdateVisibleRegion (LatLng pos)
- {
- var map = NativeMap;
- if (map == null)
- return;
- var projection = map.Projection;
- var width = Control.Width;
- var height = Control.Height;
- var ul = projection.FromScreenLocation (new global::Android.Graphics.Point (0, 0));
- var ur = projection.FromScreenLocation (new global::Android.Graphics.Point (width, 0));
- var ll = projection.FromScreenLocation (new global::Android.Graphics.Point (0, height));
- var lr = projection.FromScreenLocation (new global::Android.Graphics.Point (width, height));
- var dlat = Math.Max (Math.Abs (ul.Latitude - lr.Latitude), Math.Abs (ur.Latitude - ll.Latitude));
- var dlong = Math.Max (Math.Abs (ul.Longitude - lr.Longitude), Math.Abs (ur.Longitude - ll.Longitude));
- ((Map)Element).VisibleRegion = new MapSpan (
- new Position (
- pos.Latitude,
- pos.Longitude
- ),
- dlat,
- dlong
- );
- }
-
- void AddPins (IList pins)
+ void AddPins(IList pins)
{
- var map = NativeMap;
+ GoogleMap map = NativeMap;
if (map == null)
+ {
return;
+ }
if (_markers == null)
- _markers = new List<Marker> ();
+ {
+ _markers = new List<Marker>();
+ }
- _markers.AddRange( pins.Cast<Pin>().Select (p => {
- var pin = (Pin)p;
- var opts = new MarkerOptions ();
- opts.SetPosition (new LatLng (pin.Position.Latitude, pin.Position.Longitude));
- opts.SetTitle (pin.Label);
- opts.SetSnippet (pin.Address);
- var marker = map.AddMarker (opts);
+ _markers.AddRange(pins.Cast<Pin>().Select(p =>
+ {
+ Pin pin = p;
+ var opts = new MarkerOptions();
+ opts.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
+ opts.SetTitle(pin.Label);
+ opts.SetSnippet(pin.Address);
+ var marker = map.AddMarker(opts);
// associate pin with marker for later lookup in event handlers
pin.Id = marker.Id;
@@ -256,34 +212,20 @@ namespace Xamarin.Forms.Maps.Android
}));
}
- void RemovePins (IList pins)
- {
- var map = NativeMap;
- if (map == null)
- return;
- if (_markers == null)
- return;
-
- foreach (Pin p in pins) {
- var marker = _markers.FirstOrDefault (m => (object)m.Id == p.Id);
- if (marker == null)
- continue;
- marker.Remove ();
- _markers.Remove (marker);
- }
- }
-
- void MapOnMarkerClick (object sender, GoogleMap.InfoWindowClickEventArgs eventArgs)
+ void MapOnMarkerClick(object sender, GoogleMap.InfoWindowClickEventArgs eventArgs)
{
// clicked marker
var marker = eventArgs.Marker;
// lookup pin
Pin targetPin = null;
- for (var i = 0; i < Map.Pins.Count; i++) {
- var pin = Map.Pins[i];
+ for (var i = 0; i < Map.Pins.Count; i++)
+ {
+ Pin pin = Map.Pins[i];
if ((string)pin.Id != marker.Id)
+ {
continue;
+ }
targetPin = pin;
break;
@@ -291,30 +233,136 @@ namespace Xamarin.Forms.Maps.Android
// only consider event handled if a handler is present.
// Else allow default behavior of displaying an info window.
- targetPin?.SendTap ();
+ targetPin?.SendTap();
}
- bool _disposed;
- protected override void Dispose (bool disposing)
+ void MoveToRegion(MapSpan span, bool animate)
{
- if (disposing && !_disposed) {
- _disposed = true;
-
- var mapModel = Element as Map;
- if (mapModel != null) {
- MessagingCenter.Unsubscribe<Map, MapSpan> (this, MoveMessageName);
- ((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;
+ GoogleMap map = NativeMap;
+ if (map == null)
+ {
+ return;
+ }
+
+ span = span.ClampLatitude(85, -85);
+ var ne = new LatLng(span.Center.Latitude + span.LatitudeDegrees / 2,
+ span.Center.Longitude + span.LongitudeDegrees / 2);
+ var sw = new LatLng(span.Center.Latitude - span.LatitudeDegrees / 2,
+ span.Center.Longitude - span.LongitudeDegrees / 2);
+ CameraUpdate update = CameraUpdateFactory.NewLatLngBounds(new LatLngBounds(sw, ne), 0);
+
+ try
+ {
+ if (animate)
+ {
+ map.AnimateCamera(update);
+ }
+ else
+ {
+ map.MoveCamera(update);
}
+ }
+ catch (IllegalStateException exc)
+ {
+ System.Diagnostics.Debug.WriteLine("MoveToRegion exception: " + exc);
+ }
+ }
- var gmap = NativeMap;
- if (gmap == null)
- return;
- gmap.MyLocationEnabled = false;
- gmap.InfoWindowClick -= MapOnMarkerClick;
- gmap.Dispose ();
+ void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
+ {
+ switch (notifyCollectionChangedEventArgs.Action)
+ {
+ case NotifyCollectionChangedAction.Add:
+ AddPins(notifyCollectionChangedEventArgs.NewItems);
+ break;
+ case NotifyCollectionChangedAction.Remove:
+ RemovePins(notifyCollectionChangedEventArgs.OldItems);
+ break;
+ case NotifyCollectionChangedAction.Replace:
+ RemovePins(notifyCollectionChangedEventArgs.OldItems);
+ AddPins(notifyCollectionChangedEventArgs.NewItems);
+ break;
+ case NotifyCollectionChangedAction.Reset:
+ _markers?.ForEach(m => m.Remove());
+ _markers = null;
+ AddPins((IList)Element.Pins);
+ break;
+ case NotifyCollectionChangedAction.Move:
+ //do nothing
+ break;
+ }
+ }
+
+ void OnMoveToRegionMessage(Map s, MapSpan a)
+ {
+ MoveToRegion(a, true);
+ }
+
+ void RemovePins(IList pins)
+ {
+ GoogleMap map = NativeMap;
+ if (map == null)
+ {
+ return;
+ }
+ if (_markers == null)
+ {
+ return;
+ }
+
+ foreach (Pin p in pins)
+ {
+ var marker = _markers.FirstOrDefault(m => (object)m.Id == p.Id);
+ if (marker == null)
+ {
+ continue;
+ }
+ marker.Remove();
+ _markers.Remove(marker);
+ }
+ }
+
+ void SetMapType()
+ {
+ GoogleMap map = NativeMap;
+ if (map == null)
+ {
+ return;
}
- base.Dispose (disposing);
+ switch (Map.MapType)
+ {
+ case MapType.Street:
+ map.MapType = GoogleMap.MapTypeNormal;
+ break;
+ case MapType.Satellite:
+ map.MapType = GoogleMap.MapTypeSatellite;
+ break;
+ case MapType.Hybrid:
+ map.MapType = GoogleMap.MapTypeHybrid;
+ break;
+ default:
+ throw new ArgumentOutOfRangeException();
+ }
+ }
+
+ void UpdateVisibleRegion(LatLng pos)
+ {
+ GoogleMap map = NativeMap;
+ if (map == null)
+ {
+ return;
+ }
+ Projection projection = map.Projection;
+ int width = Control.Width;
+ int height = Control.Height;
+ LatLng ul = projection.FromScreenLocation(new global::Android.Graphics.Point(0, 0));
+ LatLng ur = projection.FromScreenLocation(new global::Android.Graphics.Point(width, 0));
+ LatLng ll = projection.FromScreenLocation(new global::Android.Graphics.Point(0, height));
+ LatLng lr = projection.FromScreenLocation(new global::Android.Graphics.Point(width, height));
+ double dlat = Math.Max(Math.Abs(ul.Latitude - lr.Latitude), Math.Abs(ur.Latitude - ll.Latitude));
+ 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);
}
}
-}
+} \ No newline at end of file