summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/MapGallery.cs
blob: dd283c3a3a4c114b140f889989f908192564675f (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Maps;

namespace Xamarin.Forms.Controls
{
	public class MapGallery : ContentPage
	{
		readonly StackLayout _stack;

		public MapGallery ()
		{
			Device.OnPlatform (iOS: () => {
				if (Device.Idiom == TargetIdiom.Tablet) {
					Padding = new Thickness (0, 0, 0, 60);
				}
			});

			var map = MakeMap ();

			map.MoveToRegion (MapSpan.FromCenterAndRadius (new Position (41.890202, 12.492049), Distance.FromMiles (0.5)));

			var searchAddress = new SearchBar { Placeholder = "Search Address" };

			searchAddress.SearchButtonPressed += async (e, a) => {
				var addressQuery = searchAddress.Text;
				searchAddress.Text = "";
				searchAddress.Unfocus ();

				var positions = (await (new Geocoder ()).GetPositionsForAddressAsync (addressQuery)).ToList ();
				if (!positions.Any ())
					return;

				var position = positions.First ();
				map.MoveToRegion (MapSpan.FromCenterAndRadius (position,
					Distance.FromMeters (4000)));
				map.Pins.Add (new Pin {
					Label = addressQuery,
					Position = position,
					Address = addressQuery
				});
			};

			var buttonAddressFromPosition = new Button { Text = "Address From Position" };
			buttonAddressFromPosition.Clicked += async (e, a) => {
				var addresses = (await (new Geocoder ()).GetAddressesForPositionAsync (new Position (41.8902, 12.4923))).ToList ();
				foreach (var ad in addresses)
					Debug.WriteLine (ad);
			};

			var buttonZoomIn = new Button { Text = "Zoom In" };
			buttonZoomIn.Clicked += (e, a) => map.MoveToRegion (map.VisibleRegion.WithZoom (5f));

			var buttonZoomOut = new Button { Text = "Zoom Out" };
			buttonZoomOut.Clicked += (e, a) => map.MoveToRegion (map.VisibleRegion.WithZoom (1 / 3f));

			var mapTypeButton = new Button { Text = "Map Type" };
			mapTypeButton.Clicked += async (e, a) => {
				var result = await DisplayActionSheet ("Select Map Type", null, null, "Street", "Satellite", "Hybrid");
				switch (result) {
				case "Street":
					map.MapType = MapType.Street;
					break;
				case "Satellite":
					map.MapType = MapType.Satellite;
					break;
				case "Hybrid":
					map.MapType = MapType.Hybrid;
					break;
				}
			};

			var buttonHome = new Button { Text = "Home" };
			buttonHome.Clicked += (a, e) => {
				map.MoveToRegion (MapSpan.FromCenterAndRadius (new Position (41.890202, 12.492049), Distance.FromMiles (0.5)));
			};

			_stack = new StackLayout {
				Spacing = 0,
				Padding = new Thickness (30, 0)
			};
			//stack.SetRowSpacing (1, 0);

			Title = "Map Gallery";

			var buttonZoomPin = new Button { Text = "Zoom Pin" };
			buttonZoomPin.Clicked += (a, e) => {
				var pos = new Position (41.011995, -8.642995);
				map.Pins.Clear ();
				map.Pins.Add (new Pin { Position = pos, Label = "Rui" });
				map.MoveToRegion (MapSpan.FromCenterAndRadius (pos, Distance.FromMiles (0.5)));
			};
			

			map.VerticalOptions = LayoutOptions.FillAndExpand;
			_stack.Children.Add (searchAddress);
			_stack.Children.Add (map);
			_stack.Children.Add (mapTypeButton);
			_stack.Children.Add (buttonZoomIn);
			_stack.Children.Add (buttonZoomOut);
			_stack.Children.Add (buttonAddressFromPosition);
			_stack.Children.Add (buttonHome);
			_stack.Children.Add (buttonZoomPin);

			Content = _stack;
		}

		public static Map MakeMap ()
		{
			Pin colosseum = null;
			Pin pantheon = null;
			Pin chapel = null;

			var map = new Map {
				IsShowingUser = false,
				Pins = {
					(colosseum = new Pin {
						Type = PinType.Place,
						Position = new Position (41.890202, 12.492049),
						Label = "Colosseum",
						Address = "Piazza del Colosseo, 00184 Rome, Province of Rome, Italy"
					}),
					(pantheon = new Pin {
						Type = PinType.Place,
						Position = new Position (41.898652, 12.476831),
						Label = "Pantheon",
						Address = "Piazza della Rotunda, 00186 Rome, Province of Rome, Italy"
					}),
					(chapel = new Pin {
						Type = PinType.Place,
						Position = new Position (41.903209, 12.454545),
						Label = "Sistine Chapel",
						Address = "Piazza della Rotunda, 00186 Rome, Province of Rome, Italy"
					})
				}
			};

			colosseum.Clicked += PinClicked;
			pantheon.Clicked += PinClicked;
			chapel.Clicked += PinClicked;
			return map;
		}

		static void PinClicked (object sender, EventArgs e)
		{
			// yea!
		}
	}
}