-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainPage.xaml.cs
105 lines (89 loc) · 4.77 KB
/
MainPage.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
using BikeAvailability.ViewModel;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.RealTime;
using Esri.ArcGISRuntime.UI;
namespace BikeAvailability;
public partial class MainPage : ContentPage, IQueryAttributable
{
// Images to use for the "add favorite" and "remove from favorites" buttons.
private readonly string _makeFavoriteImage = "https://raw.githubusercontent.com/ThadT/bike-rental-stations-maui/main/MakeFavorite.png";
private readonly string _unFavoriteImage = "https://raw.githubusercontent.com/ThadT/bike-rental-stations-maui/main/UnFavorite.png";
private readonly CityBikesViewModel _vm;
public MainPage(CityBikesViewModel vm)
{
InitializeComponent();
BindingContext = vm;
_vm = vm;
}
private async void MapViewTapped(object sender, GeoViewInputEventArgs e)
{
// Close any currently open callout.
mapView.DismissCallout();
var dynamicEntityLayer = mapView.Map.OperationalLayers.OfType<DynamicEntityLayer>().FirstOrDefault();
// Identify city graphics if they are displayed on the map.
if (mapView.GraphicsOverlays["CitiesOverlay"].MaxScale < mapView.GetCurrentViewpoint(ViewpointType.CenterAndScale).TargetScale)
{
var results = await mapView.IdentifyGraphicsOverlayAsync(mapView.GraphicsOverlays["CitiesOverlay"], e.Position, 4, false);
if (results.Graphics.Count == 0) { return; }
// Load the bike stations for the city that was clicked.
var cityGraphic = results.Graphics[0];
var cityName = cityGraphic.Attributes["Name"].ToString();
CityPicker.SelectedItem = cityName;
}
else if (dynamicEntityLayer != null)
{
// Identify a bike station from the tap.
var results = await mapView.IdentifyLayerAsync(dynamicEntityLayer, e.Position, 4, false, 1);
if (results.GeoElements.Count == 0 || results.GeoElements[0] is not DynamicEntityObservation bikeStation) { return; }
// Get a callout definition from the view model.
var calloutDef = _vm.GetCalloutDefinitionForStation(bikeStation, _unFavoriteImage, _makeFavoriteImage);
// Set the button click to add/remove this station as a favorite.
calloutDef.OnButtonClick = (tag) =>
{
// Add or remove this station from the favorites list.
var isFavorite = _vm.ToggleIsFavorite(tag as DynamicEntity, CityPicker.SelectedItem.ToString());
// Apply the correct image to the callout and show it again.
calloutDef.ButtonImage = isFavorite ?
new RuntimeImage(new Uri(_unFavoriteImage)) :
new RuntimeImage(new Uri(_makeFavoriteImage));
mapView.ShowCalloutAt(bikeStation.Geometry as MapPoint, calloutDef);
};
// Show the callout.
mapView.ShowCalloutAt(bikeStation.Geometry as MapPoint, calloutDef);
}
}
private async void CityPicker_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the selected city name and pass it to the VM to show the stations for that city.
var cityName = CityPicker.SelectedItem.ToString();
var viewpoint = await _vm.ShowBikeStations(cityName);
// Zoom to the extent of the selected city.
mapView.SetViewpoint(viewpoint);
// Show bike inventory for the entire city.
BikeInventoryPanel.IsVisible = true;
}
// Handle navigation from a button click in the favorites page to a station.
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
if (query["favorite"] is DynamicEntity favorite)
{
var location = favorite.Geometry as MapPoint;
mapView.SetViewpoint(new Viewpoint(location, 10000));
// Close any currently open callout.
mapView.DismissCallout();
var calloutDef = CityBikesViewModel.GetCalloutDefinitionForStation(favorite, _unFavoriteImage);
calloutDef.OnButtonClick = (tag) =>
{
// Add or remove this station from the favorites list.
var isFavorite = _vm.ToggleIsFavorite(tag as DynamicEntity, CityPicker.SelectedItem.ToString());
// Apply the correct image to the callout and show it again.
calloutDef.ButtonImage = isFavorite ?
new RuntimeImage(new Uri(_unFavoriteImage)) :
new RuntimeImage(new Uri(_makeFavoriteImage));
mapView.ShowCalloutAt(location, calloutDef);
};
mapView.ShowCalloutAt(location, calloutDef);
}
}
}