summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.Tizen/MapRenderer.cs
blob: c04f6cc814434e4b7df9f5f4f4a71404ef248b73 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Tizen.Location;
using Tizen.Maps;
using Xamarin.Forms.Platform.Tizen;
using TPin = Tizen.Maps.Pin;

namespace Xamarin.Forms.Maps.Tizen
{
	public class MapRenderer : ViewRenderer<Map, MapView>
	{
		const string MoveMessageName = "MapMoveToRegion";
		const int BaseZoomLevel = 2;

		bool _disposed;
		Marker _marker;
		bool _isLocatorStarted = false;
		Lazy<Locator> _locator = new Lazy<Locator>(InitializeLocator);
		Dictionary<Pin, MapObject> _pins = new Dictionary<Pin, MapObject>();

		static Locator InitializeLocator()
		{
			var locator = new Locator(LocationType.Hybrid)
			{
				// Set the default interval to 15s same as UWP
				Interval = 15
			};
			return locator;
		}

		public MapRenderer()
		{
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
		{
			if (Control == null)
			{
				var mapControl = new MapView(Platform.Tizen.Forms.Context.MainWindow, FormsMaps.MapService);

				mapControl.RenderPost += OnVisibleRegionChanged;

				SetNativeControl(mapControl);

				if (Element.LastMoveToRegion != null)
				{
					OnMoveToRegion(null, Element.LastMoveToRegion);
				}
			}

			if (e.OldElement != null)
			{
				((ObservableCollection<Pin>)e.OldElement.Pins).CollectionChanged -= OnCollectionChanged;

				MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
			}
			if (e.NewElement != null)
			{
				((ObservableCollection<Pin>)e.NewElement.Pins).CollectionChanged += OnCollectionChanged;
				if (e.NewElement.Pins.Count > 0)
				{
					AddPins(e.NewElement.Pins);
				}

				MessagingCenter.Subscribe<Map, MapSpan>(this, MoveMessageName, OnMoveToRegion, e.NewElement);

				UpdateMapType();
				UpdateHasScrollEnabled();
				UpdateHasZoomEnabled();
				UpdateIsShowingUser();
				UpdateVisibleRegion();
			}
			base.OnElementChanged(e);
		}

		protected override void Dispose(bool disposing)
		{
			if (_disposed)
			{
				return;
			}

			_disposed = true;

			if (disposing)
			{
				MessagingCenter.Unsubscribe<Map, MapSpan>(this, "MapMoveToRegion");
				if (Element != null)
				{
					((ObservableCollection<Pin>)Element.Pins).CollectionChanged -= OnCollectionChanged;
				}
				Control.RenderPost -= OnVisibleRegionChanged;
				Control.Unrealize();
			}
			base.Dispose(disposing);
		}

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

			if (e.PropertyName == Map.MapTypeProperty.PropertyName)
				UpdateMapType();
			else if (e.PropertyName == Map.IsShowingUserProperty.PropertyName)
				UpdateIsShowingUser();
			else if (e.PropertyName == Map.HasScrollEnabledProperty.PropertyName)
				UpdateHasScrollEnabled();
			else if (e.PropertyName == Map.HasZoomEnabledProperty.PropertyName)
				UpdateHasZoomEnabled();
		}

		void OnMoveToRegion(Map map, MapSpan span)
		{
			UpdateVisibleRegion();

			int latitudeZoomFactor = GetZoomFactor(span.LatitudeDegrees, 90.0);
			int longitudeZoomFactor = GetZoomFactor(span.LongitudeDegrees, 180.0);

			Control.Center = new Geocoordinates(span.Center.Latitude, span.Center.Longitude); ;
			Control.ZoomLevel = BaseZoomLevel + Math.Min(latitudeZoomFactor, longitudeZoomFactor);
			UpdateVisibleRegion();
		}

		int GetZoomFactor(double degree, double targetDegree)
		{
			int factor = 0;
			double tempDegree = degree;
			while (true)
			{
				tempDegree = tempDegree * 2;
				if (tempDegree > targetDegree)
					break;
				factor++;
			}
			return factor;
		}

		void OnVisibleRegionChanged(object sender, EventArgs e)
		{
			UpdateVisibleRegion();
		}

		void UpdateVisibleRegion()
		{
			int width = Control.Geometry.Width;
			int height = Control.Geometry.Height;
			int x = Control.Geometry.X;
			int y = Control.Geometry.Y;

			Geocoordinates ul = Control.ScreenToGeolocation(new ElmSharp.Point { X = x, Y = y});
			Geocoordinates ur = Control.ScreenToGeolocation(new ElmSharp.Point { X = x + width, Y = y});
			Geocoordinates ll = Control.ScreenToGeolocation(new ElmSharp.Point { X = x, Y = y + height });
			Geocoordinates lr = Control.ScreenToGeolocation(new ElmSharp.Point { X = x + width, Y = y + 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(Control.Center.Latitude, Control.Center.Longitude), dlat, dlong);
		}


		void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.NewItems != null)
			{
				AddPins(e.NewItems);
			}
			if (e.OldItems != null)
			{
				RemovePins(e.OldItems);
			}
			if (e.Action == NotifyCollectionChangedAction.Reset)
			{
				ClearPins();
			}
		}

		void AddPins(IEnumerable pins)
		{
			foreach (Pin pin in pins)
			{
				var coordinates = new Geocoordinates(pin.Position.Latitude, pin.Position.Longitude);
				var nativePin = new TPin(coordinates);
				nativePin.Clicked += (s, e) =>
				{
					pin.SendTap();
				};
				Control.Add(nativePin);
				_pins.Add(pin, nativePin);
			}
		}

		void RemovePins(IEnumerable pins)
		{
			foreach (Pin pin in pins)
			{
				if (_pins.ContainsKey(pin))
				{
					Control.Remove(_pins[pin]);
					_pins.Remove(pin);
				}
			}
		}

		void ClearPins()
		{
			foreach (var pin in _pins)
			{
				Control.Remove(pin.Value);
			}
			_pins.Clear();
		}

		void UpdateHasZoomEnabled()
		{
			if (Element.HasZoomEnabled == true)
				Control.ZoomChanged += Dummy;
			else
				Control.ZoomChanged -= Dummy;
		}

		void UpdateHasScrollEnabled()
		{
			if (Element.HasScrollEnabled == true)
				Control.Scrolled += Dummy;
			else
				Control.Scrolled -= Dummy;
		}

		void Dummy(object sender, MapGestureEventArgs e)
		{
			// The implementation of Tizen.Maps needs to be changed to remove this method
		}

		void ApplyIsShowingUser(Geocoordinates coordinates)
		{
			if (_marker == null)
			{
				_marker = new Sticker(coordinates);
				_marker.IsVisible = false;
				Control.Add(_marker);
			}
			_marker.Coordinates = coordinates;

			if (!_marker.IsVisible)
			{
				_marker.IsVisible = true;
				Control.Center = coordinates;
				Control.ZoomLevel = 13;
			}
		}
		void UpdateIsShowingUser()
		{
			if (Element.IsShowingUser)
			{
				_locator.Value.LocationChanged += OnLocationChanged;
				if (!_isLocatorStarted)
				{
					_locator.Value.Start();
					_isLocatorStarted = true;
				}
			}
			else
			{
				if (_locator.IsValueCreated)
				{
					_locator.Value.LocationChanged -= OnLocationChanged;
					_locator.Value.Stop();
					_isLocatorStarted = false;
				}
				if (_marker != null)
					_marker.IsVisible = false;
			}
		}

		void OnLocationChanged(object sender, LocationChangedEventArgs e)
		{
			ApplyIsShowingUser(new Geocoordinates(e.Location.Latitude, e.Location.Longitude));
		}

		void UpdateMapType()
		{
			switch (Element.MapType)
			{
				case MapType.Street:
					Control.MapType = MapTypes.Normal;
					break;
				case MapType.Satellite:
					Control.MapType = MapTypes.Satellite;
					break;
				case MapType.Hybrid:
					Control.MapType = MapTypes.Hybrid;
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}
		}
	}
}