summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.UWP/PushPin.cs
blob: ea07d7314b0172cbf027a4fa0f1663ca68e5253d (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
using System;
using System.ComponentModel;
using Windows.Devices.Geolocation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Maps;
using Windows.UI.Xaml.Input;

#if WINDOWS_UWP

namespace Xamarin.Forms.Maps.UWP
#else

namespace Xamarin.Forms.Maps.WinRT
#endif
{
	internal class PushPin : ContentControl
	{
		readonly Pin _pin;

		internal PushPin(Pin pin)
		{
			if (pin == null)
				throw new ArgumentNullException();

			ContentTemplate = Windows.UI.Xaml.Application.Current.Resources["PushPinTemplate"] as Windows.UI.Xaml.DataTemplate;
			DataContext = Content = _pin = pin;

			UpdateLocation();

			Loaded += PushPinLoaded;
			Unloaded += PushPinUnloaded;
			Tapped += PushPinTapped;
		}

		void PushPinLoaded(object sender, RoutedEventArgs e)
		{
			_pin.PropertyChanged += PinPropertyChanged;
		}

		void PushPinUnloaded(object sender, RoutedEventArgs e)
		{
			_pin.PropertyChanged -= PinPropertyChanged;
			Tapped -= PushPinTapped;
		}

		void PinPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == Pin.PositionProperty.PropertyName)
				UpdateLocation();
		}

		void PushPinTapped(object sender, TappedRoutedEventArgs e)
		{
			_pin.SendTap();
		}

		void UpdateLocation()
		{
			var anchor = new Windows.Foundation.Point(0.65, 1);
			var location = new Geopoint(new BasicGeoposition
			{
				Latitude = _pin.Position.Latitude,
				Longitude = _pin.Position.Longitude
			});
			MapControl.SetLocation(this, location);
			MapControl.SetNormalizedAnchorPoint(this, anchor);
		}
	}
}