/
MapPageViewModel.cs
124 lines (104 loc) · 4.03 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Prism.Windows.Mvvm;
using Prism.Windows.Navigation;
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 ILocationService _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 string _mapServiceToken;
public string MapServiceToken
{
get { return _mapServiceToken; }
set { Param_Setter(ref _mapServiceToken, value); }
}
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); }
}
private ObservableCollection<MapIcon> _mapIcons = new ObservableCollection<MapIcon>();
public ObservableCollection<MapIcon> MapIcons
{
get { return _mapIcons; }
set { Param_Setter(ref _mapIcons, value); }
}
public MapPageViewModel(ILocationService locationServiceInstance)
{
_locationService = locationServiceInstance;
Center = new Geopoint(_defaultPosition);
ZoomLevel = DefaultZoomLevel;
// TODO WTS: Set your map service token. If you don't have one, request from https://www.bingmapsportal.com/
// MapServiceToken = string.Empty;
}
public override async void OnNavigatedTo(NavigatedToEventArgs e, Dictionary<string, object> viewModelState)
{
base.OnNavigatedTo(e, viewModelState);
if (_locationService != null)
{
_locationService.PositionChanged += LocationServicePositionChanged;
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);
}
}
var mapIcon = new MapIcon()
{
Location = Center,
NormalizedAnchorPoint = new Point(0.5, 1.0),
Title = "Map_YourLocation".GetLocalized(),
Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/map.png")),
ZIndex = 0
};
MapIcons.Add(mapIcon);
}
public override void OnNavigatingFrom(NavigatingFromEventArgs e, Dictionary<string, object> viewModelState, bool suspending)
{
base.OnNavigatingFrom(e, viewModelState, suspending);
if (_locationService != null)
{
_locationService.PositionChanged -= LocationServicePositionChanged;
_locationService.StopListening();
}
}
private void LocationServicePositionChanged(object sender, Geoposition geoposition)
{
if (geoposition != null)
{
Center = geoposition.Coordinate.Point;
}
}
}
}