summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.WP8/MapRenderer.cs
blob: 34cb05f15f3bad1b266e4c3e81c998245e1ec647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Device.Location;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Maps;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Toolkit;
using Xamarin.Forms.Platform.WinPhone;

namespace Xamarin.Forms.Maps.WP8
{
	public class
		MapRenderer : ViewRenderer<Map, Microsoft.Phone.Maps.Controls.Map>
	{
		bool _firstZoomLevelChangeFired;
		MapLayer _pushPinLayer;
		MapLayer _userLocationLayer;

		protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
		{
			base.OnElementChanged(e);

			SetNativeControl(new Microsoft.Phone.Maps.Controls.Map());
			UpdateMapType();

			Control.Loaded += ControlOnLoaded;

			_pushPinLayer = new MapLayer();
			Control.Layers.Add(_pushPinLayer);

			_userLocationLayer = new MapLayer();
			Control.Layers.Add(_userLocationLayer);

			Control.ViewChanged += (s, a) => UpdateVisibleRegion();
			Control.ZoomLevelChanged += (sender, args) => UpdateVisibleRegion();
			Control.CenterChanged += (s, a) => UpdateVisibleRegion();
			//Control.ViewChangeOnFrame += (s, a) => UpdateVisibleRegion ();

			MessagingCenter.Subscribe<Map, MapSpan>(this, "MapMoveToRegion", (s, a) => MoveToRegion(a), Element);

			((ObservableCollection<Pin>)Element.Pins).CollectionChanged += OnCollectionChanged;

			if (Element.Pins.Any())
				LoadPins();

			UpdateShowUserLocation();
		}

		async void UpdateShowUserLocation()
		{
			if (Element.IsShowingUser)
			{
				var myGeolocator = new Geolocator();

				if (myGeolocator.LocationStatus != PositionStatus.NotAvailable &&
				    myGeolocator.LocationStatus != PositionStatus.Disabled)
				{
					var userPosition = await myGeolocator.GetGeopositionAsync();
					if (userPosition?.Coordinate != null)
						LoadUserPosition(userPosition.Coordinate, true);
				}
			}
			else if (_userLocationLayer.Count > 0)
				_userLocationLayer.Clear();
		}

		void ControlOnLoaded(object sender, RoutedEventArgs routedEventArgs)
		{
			MapsSettings.ApplicationContext.ApplicationId = FormsMaps.ApplicationId;
			MapsSettings.ApplicationContext.AuthenticationToken = FormsMaps.AuthenticationToken;
		}

		void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action)
			{
				case NotifyCollectionChangedAction.Add:
					foreach (Pin pin in e.NewItems)
						LoadPin(pin);
					break;
				case NotifyCollectionChangedAction.Move:
					// no matter
					break;
				case NotifyCollectionChangedAction.Remove:
					foreach (Pin pin in e.OldItems)
						RemovePin(pin);
					break;
				case NotifyCollectionChangedAction.Replace:
					foreach (Pin pin in e.OldItems)
						RemovePin(pin);
					foreach (Pin pin in e.NewItems)
						LoadPin(pin);
					break;
				case NotifyCollectionChangedAction.Reset:
					_pushPinLayer.Clear();
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}
		}

		void UpdateVisibleRegion()
		{
			if (!_firstZoomLevelChangeFired)
			{
				MoveToRegion(Element.LastMoveToRegion, MapAnimationKind.None);
				_firstZoomLevelChangeFired = true;
				return;
			}

			var center = new Position(Control.Center.Latitude, Control.Center.Longitude);
			var topLeft = Control.ConvertViewportPointToGeoCoordinate(new System.Windows.Point(0, 0));
			var bottomRight =
				Control.ConvertViewportPointToGeoCoordinate(new System.Windows.Point(Control.ActualWidth, Control.ActualHeight));
			if (topLeft == null || bottomRight == null)
				return;

			var boundingRegion = LocationRectangle.CreateBoundingRectangle(topLeft, bottomRight);
			var result = new MapSpan(center, boundingRegion.HeightInDegrees, boundingRegion.WidthInDegrees);
			Element.VisibleRegion = result;
		}

		void LoadPins()
		{
			foreach (var pin in Element.Pins)
				LoadPin(pin);
		}

		void LoadPin(Pin pin)
		{
			var location = new GeoCoordinate(pin.Position.Latitude, pin.Position.Longitude);
			var pushPin = new Pushpin
			{
				Content = pin.Label,
				PositionOrigin = new System.Windows.Point(0, 1),
				Tag = pin
			};

			pushPin.Tap += PinTap;

			var pushPinOverlay = new MapOverlay
			{
				Content = pushPin,
				GeoCoordinate = location,
				PositionOrigin = new System.Windows.Point(0, 1)
			};
			_pushPinLayer.Add(pushPinOverlay);
		}

		void PinTap(object sender, GestureEventArgs e)
		{
			var pushPin = (Pushpin)sender;
			var pin = (Pin)pushPin.Tag;
			pin.SendTap();
		}

		void RemovePin(Pin pin)
		{
			var child = _pushPinLayer.FirstOrDefault(p => ((Pushpin)p.Content).Tag == (object)pin);
			if (child != null)
				_pushPinLayer.Remove(child);
		}

		void MoveToRegion(MapSpan span, MapAnimationKind animation = MapAnimationKind.Parabolic)
		{
			// FIXME
			var center = new GeoCoordinate(span.Center.Latitude, span.Center.Longitude);
			var location = new LocationRectangle(center, span.LongitudeDegrees, span.LatitudeDegrees);
			Control.SetView(location, animation);
		}

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return new SizeRequest(new Size(100, 100));
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Map.MapTypeProperty.PropertyName)
				UpdateMapType();
			if (e.PropertyName == Map.IsShowingUserProperty.PropertyName)
				UpdateShowUserLocation();
		}

		void UpdateMapType()
		{
			switch (Element.MapType)
			{
				case MapType.Street:
					Control.CartographicMode = MapCartographicMode.Road;
					break;
				case MapType.Satellite:
					Control.CartographicMode = MapCartographicMode.Aerial;
					break;
				case MapType.Hybrid:
					Control.CartographicMode = MapCartographicMode.Hybrid;
					break;
			}
		}

		void LoadUserPosition(Geocoordinate userPosition, bool center = false)
		{
			_userLocationLayer.Clear();

			var userCoordinate = new GeoCoordinate
				(
				userPosition.Latitude,
				userPosition.Longitude,
				userPosition.Altitude ?? double.NaN,
				userPosition.Accuracy,
				userPosition.AltitudeAccuracy ?? double.NaN,
				userPosition.Speed ?? double.NaN,
				userPosition.Heading ?? double.NaN
				);

			//make some preety?
			var userPositionCircle = new Ellipse
			{
				Fill = new SolidColorBrush(Colors.Blue),
				Height = 20,
				Width = 20,
				Opacity = 50
			};

			var userPostionOverlay = new MapOverlay
			{
				Content = userPositionCircle,
				PositionOrigin = new System.Windows.Point(0.5, 0.5),
				GeoCoordinate = userCoordinate
			};

			_userLocationLayer.Add(userPostionOverlay);

			if (center)
			{
				Control.Center = userCoordinate;
				Control.ZoomLevel = 13;
			}
		}
	}
}