/
MapPageViewModel.cs
102 lines (87 loc) · 3.16 KB
/
MapPageViewModel.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
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls.Maps;
using Param_ItemNamespace.Helpers;
using Param_ItemNamespace.Services;
namespace Param_ItemNamespace.ViewModels
{
public class MapPageViewModel : System.ComponentModel.INotifyPropertyChanged
{
// TODO WTS: Set your preferred default zoom level
private const double DefaultZoomLevel = 17;
private readonly LocationService _locationService;
// TODO WTS: Set your preferred default location if a geolock can't be found.
private readonly BasicGeoposition _defaultPosition = new BasicGeoposition()
{
Latitude = 47.609425,
Longitude = -122.3417
};
private double _zoomLevel;
public double ZoomLevel
{
get { return _zoomLevel; }
set { Param_Setter(ref _zoomLevel, value); }
}
private Geopoint _center;
public Geopoint Center
{
get { return _center; }
set { Param_Setter(ref _center, value); }
}
public MapPageViewModel()
{
_locationService = new LocationService();
Center = new Geopoint(_defaultPosition);
ZoomLevel = DefaultZoomLevel;
}
public async Task InitializeAsync(MapControl map)
{
if (_locationService != null)
{
_locationService.PositionChanged += LocationService_PositionChanged;
var initializationSuccessful = await _locationService.InitializeAsync();
if (initializationSuccessful)
{
await _locationService.StartListeningAsync();
}
if (initializationSuccessful && _locationService.CurrentPosition != null)
{
Center = _locationService.CurrentPosition.Coordinate.Point;
}
else
{
Center = new Geopoint(_defaultPosition);
}
}
if (map != null)
{
// TODO WTS: Set your map service token. If you don't have one, request from https://www.bingmapsportal.com/
// map.MapServiceToken = string.Empty;
AddMapIcon(map, Center, "Map_YourLocation".GetLocalized());
}
}
private void LocationService_PositionChanged(object sender, Geoposition geoposition)
{
if (geoposition != null)
{
Center = geoposition.Coordinate.Point;
}
}
private void AddMapIcon(MapControl map, Geopoint position, string title)
{
var mapIcon = new MapIcon()
{
Location = position,
NormalizedAnchorPoint = new Point(0.5, 1.0),
Title = title,
Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/map.png")),
ZIndex = 0
};
map.MapElements.Add(mapIcon);
}
}
}