-
Notifications
You must be signed in to change notification settings - Fork 340
/
BasicMapPage.xaml.cs
179 lines (153 loc) · 6.55 KB
/
BasicMapPage.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps;
using Xamarin.Essentials;
namespace XFGoogleMapSample
{
public partial class BasicMapPage : ContentPage
{
public BasicMapPage()
{
InitializeComponent();
// MapTypes
var mapTypeValues = new List<MapType>();
foreach (var mapType in Enum.GetValues(typeof(MapType)))
{
mapTypeValues.Add((MapType)mapType);
pickerMapType.Items.Add(Enum.GetName(typeof(MapType), mapType));
}
pickerMapType.SelectedIndexChanged += (sender, e) =>
{
map.MapType = mapTypeValues[pickerMapType.SelectedIndex];
};
pickerMapType.SelectedIndex = 0;
// MyLocationEnabled
switchMyLocationEnabled.Toggled += async (sender, e) =>
{
var status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
await this.DisplayAlert("Permission Error", "This feature needs Location permission.", "CLOSE");
}
map.MyLocationEnabled = e.Value;
};
switchMyLocationEnabled.IsToggled = map.MyLocationEnabled;
// IsTrafficEnabled
switchIsTrafficEnabled.Toggled += (sender, e) =>
{
map.IsTrafficEnabled = e.Value;
};
switchIsTrafficEnabled.IsToggled = map.IsTrafficEnabled;
// IndoorEnabled
switchIndoorEnabled.Toggled += (sender, e) =>
{
map.IsIndoorEnabled = e.Value;
};
switchIndoorEnabled.IsToggled = map.IsIndoorEnabled;
// CompassEnabled
switchCompassEnabled.Toggled += (sender, e) =>
{
map.UiSettings.CompassEnabled = e.Value;
};
switchCompassEnabled.IsToggled = map.UiSettings.CompassEnabled;
// RotateGesturesEnabled
switchRotateGesturesEnabled.Toggled += (sender, e) =>
{
map.UiSettings.RotateGesturesEnabled = e.Value;
};
switchRotateGesturesEnabled.IsToggled = map.UiSettings.RotateGesturesEnabled;
// MyLocationButtonEnabled
switchMyLocationButtonEnabled.Toggled += (sender, e) =>
{
map.UiSettings.MyLocationButtonEnabled = e.Value;
};
switchMyLocationButtonEnabled.IsToggled = map.UiSettings.MyLocationButtonEnabled;
// IndoorLevelPickerEnabled
switchIndoorLevelPickerEnabled.Toggled += (sender, e) =>
{
map.UiSettings.IndoorLevelPickerEnabled = e.Value;
};
switchIndoorLevelPickerEnabled.IsToggled = map.UiSettings.IndoorLevelPickerEnabled;
// ScrollGesturesEnabled
switchScrollGesturesEnabled.Toggled += (sender, e) =>
{
map.UiSettings.ScrollGesturesEnabled = e.Value;
};
switchScrollGesturesEnabled.IsToggled = map.UiSettings.ScrollGesturesEnabled;
// TiltGesturesEnabled
switchTiltGesturesEnabled.Toggled += (sender, e) =>
{
map.UiSettings.TiltGesturesEnabled = e.Value;
};
switchTiltGesturesEnabled.IsToggled = map.UiSettings.TiltGesturesEnabled;
// ZoomControlsEnabled
switchZoomControlsEnabled.Toggled += (sender, e) =>
{
map.UiSettings.ZoomControlsEnabled = e.Value;
};
switchZoomControlsEnabled.IsToggled = map.UiSettings.ZoomControlsEnabled;
// ZoomGesturesEnabled
switchZoomGesturesEnabled.Toggled += (sender, e) =>
{
map.UiSettings.ZoomGesturesEnabled = e.Value;
};
switchZoomGesturesEnabled.IsToggled = map.UiSettings.ZoomGesturesEnabled;
// Map Clicked
map.MapClicked += (sender, e) =>
{
var lat = e.Point.Latitude.ToString("0.000");
var lng = e.Point.Longitude.ToString("0.000");
this.DisplayAlert("MapClicked", $"{lat}/{lng}", "CLOSE");
};
// Map Long clicked
map.MapLongClicked += (sender, e) =>
{
var lat = e.Point.Latitude.ToString("0.000");
var lng = e.Point.Longitude.ToString("0.000");
this.DisplayAlert("MapLongClicked", $"{lat}/{lng}", "CLOSE");
};
// Map MyLocationButton clicked
map.MyLocationButtonClicked += (sender, args) =>
{
args.Handled = switchHandleMyLocationButton.IsToggled;
if (switchHandleMyLocationButton.IsToggled)
{
this.DisplayAlert("MyLocationButtonClicked",
"If set MyLocationButtonClickedEventArgs.Handled = true then skip obtain current location",
"OK");
}
};
map.CameraChanged += (sender, args) =>
{
var p = args.Position;
labelStatus.Text = $"Lat={p.Target.Latitude:0.00}, Long={p.Target.Longitude:0.00}, Zoom={p.Zoom:0.00}, Bearing={p.Bearing:0.00}, Tilt={p.Tilt:0.00}";
};
// Geocode
buttonGeocode.Clicked += async (sender, e) =>
{
var geocoder = new Xamarin.Forms.GoogleMaps.Geocoder();
var positions = await geocoder.GetPositionsForAddressAsync(entryAddress.Text);
if (positions.Count() > 0)
{
var pos = positions.First();
map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMeters(5000)));
var reg = map.VisibleRegion;
var format = "0.00";
labelStatus.Text = $"Center = {reg.Center.Latitude.ToString(format)}, {reg.Center.Longitude.ToString(format)}";
}
else
{
await this.DisplayAlert("Not found", "Geocoder returns no results", "Close");
}
};
// Snapshot
buttonTakeSnapshot.Clicked += async (sender, e) =>
{
var stream = await map.TakeSnapshot();
imageSnapshot.Source = ImageSource.FromStream(() => stream);
};
}
}
}