Skip to content

Commit de05508

Browse files
committed
Highlight selected trail on map screen
1 parent 4121901 commit de05508

File tree

7 files changed

+73
-45
lines changed

7 files changed

+73
-45
lines changed

src/Tracked.Android/Renderers/CustomMapRenderer.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Xamarin.Forms.Maps;
99
using Xamarin.Forms.Maps.Android;
1010
using Xamarin.Forms.Platform.Android;
11-
using LatLng = Android.Gms.Maps.Model.LatLng;
1211

1312
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
1413
namespace Tracked.Droid.Renderers {
@@ -33,9 +32,7 @@ protected override void OnMapReady(GoogleMap map) {
3332
}
3433

3534
protected override MarkerOptions CreateMarker(Pin pin) {
36-
var marker = new MarkerOptions();
37-
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
38-
marker.SetTitle(pin.Label);
35+
var marker = base.CreateMarker(pin);
3936

4037
if (pin is MapPin customMapPin) {
4138
if (customMapPin.IsSpeedPin) {
@@ -51,5 +48,19 @@ protected override MarkerOptions CreateMarker(Pin pin) {
5148

5249
return marker;
5350
}
51+
52+
protected override PolylineOptions CreatePolylineOptions(Xamarin.Forms.Maps.Polyline polyline) {
53+
if (polyline is MapPolyline positionPolyline) {
54+
positionPolyline.GenerateGeoPath();
55+
}
56+
57+
var options = base.CreatePolylineOptions(polyline);
58+
59+
if (polyline is MapPolyline mapPolyline) {
60+
options.InvokeZIndex(mapPolyline.ZIndex);
61+
}
62+
63+
return options;
64+
}
5465
}
5566
}

src/Tracked/Models/MapPolyline.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Shared.Interfaces;
3-
using Xamarin.Forms;
4+
using Xamarin.Forms.Maps;
45

56
namespace Tracked.Models {
6-
public class MapPolyline {
7-
public IEnumerable<ILatLng> Positions { get; set; }
8-
public Color Colour { get; set; }
9-
public float Width { get; set; }
7+
public class MapPolyline : Polyline {
8+
public int ZIndex { get; set; } = 1;
9+
public IEnumerable<ILatLng> Positions { get; set; } = new List<ILatLng>();
10+
11+
public void GenerateGeoPath() {
12+
if (Geopath.Any()) {
13+
return;
14+
}
15+
16+
foreach (var position in Positions) {
17+
Geopath.Add(new Position(position.Latitude, position.Longitude));
18+
}
19+
}
1020
}
1121
}

src/Tracked/Screens/MapViewModelBase.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,20 @@ private void CreatePolylines() {
9797
}
9898
}
9999

100-
public void CreatePolyline(MapPolyline polyline) {
100+
protected void CreatePolyline(MapPolyline polyline) {
101101
if (polyline.Positions.Count() <= 1) {
102102
return;
103103
}
104104

105-
Polyline polylineElement = new Polyline();
105+
map.MapElements.Add(polyline);
106+
}
106107

107-
foreach (var position in polyline.Positions) {
108-
polylineElement.Geopath.Add(new Position(position.Latitude, position.Longitude));
108+
protected void RemovePolyline(MapPolyline polyline) {
109+
if (polyline == null) {
110+
return;
109111
}
110112

111-
polylineElement.StrokeColor = polyline.Colour;
112-
polylineElement.StrokeWidth = polyline.Width;
113-
114-
map.MapElements.Add(polylineElement);
113+
map.MapElements.Remove(polyline);
115114
}
116115

117116
public IEnumerable<MapType> MapTypes => (IEnumerable<MapType>)Enum.GetValues(typeof(MapType));
@@ -125,5 +124,11 @@ public MapType MapType {
125124
}
126125
}
127126
}
127+
128+
protected void GoToLocation(ILatLng midpoint, Distance distance) {
129+
var position = new Position(midpoint.Latitude, midpoint.Longitude);
130+
131+
map.MoveToRegion(MapSpan.FromCenterAndRadius(position, distance));
132+
}
128133
}
129134
}
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using System.Collections.Generic;
2+
using System.Drawing;
3+
using System.Linq;
24
using Shared.Dtos;
35
using Tracked.Contexts;
6+
using Tracked.Models;
7+
using Tracked.Utilities;
48
using Xamarin.Forms.Maps;
59

610
namespace Tracked.Screens.Rides {
711
public class MapScreenViewModel : RideMapViewModelBase {
812
private readonly RideDto ride;
9-
private Polyline selectedTrailLine;
13+
private MapPolyline selectedTrailLine;
1014

1115
public MapScreenViewModel(MainContext context, RideDto ride)
1216
: base(context) {
@@ -19,28 +23,25 @@ public MapScreenViewModel(MainContext context, RideDto ride)
1923
public IList<TrailAttemptDto> TrailAttempts => ride.TrailAttempts;
2024

2125
public void HighlightTrail(TrailAttemptDto attempt) {
22-
//if (selectedTrailLine != null) {
23-
// Map.MapElements.Remove(selectedTrailLine);
24-
//}
26+
if (selectedTrailLine != null) {
27+
RemovePolyline(selectedTrailLine);
28+
}
2529

26-
//selectedTrailLine = new Polyline();
30+
selectedTrailLine = new MapPolyline {
31+
StrokeColor = Color.Blue,
32+
StrokeWidth = 20f,
33+
ZIndex = 0,
34+
Positions = ride.Locations
35+
.Where(i => i.Timestamp >= attempt.StartUtc)
36+
.Where(i => i.Timestamp <= attempt.EndUtc)
37+
.ToList(),
38+
};
2739

28-
//var latLngs = ride.Locations
29-
// .Where(i => i.Timestamp >= attempt.StartUtc)
30-
// .Where(i => i.Timestamp <= attempt.EndUtc);
40+
CreatePolyline(selectedTrailLine);
3141

42+
var midpoint = selectedTrailLine.Positions.Midpoint();
3243

33-
//foreach (var latLng in latLngs) {
34-
// selectedTrailLine.Geopath.Add(new Position(latLng.Latitude, latLng.Longitude));
35-
//}
36-
37-
//selectedTrailLine.StrokeColor = Color.Blue;
38-
//selectedTrailLine.StrokeWidth = 20f;
39-
40-
//Map.MapElements.Add(selectedTrailLine);
41-
42-
//var centre = latLngs.Midpoint();
43-
//Map.MoveToRegion(MapSpan.FromCenterAndRadius(centre, Distance.FromMiles(.25)));
44+
GoToLocation(midpoint, Distance.FromMiles(.25));
4445
}
4546
}
4647
}

src/Tracked/Screens/Rides/RideMapViewModelBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ protected override IEnumerable<MapPolyline> GetPolylines() {
5555

5656
private MapPolyline CreatePolyline(IEnumerable<ILatLng> polylineLocations, Color lastColour) {
5757
return new MapPolyline {
58-
Colour = lastColour,
59-
Width = 10,
58+
StrokeColor = lastColour,
59+
StrokeWidth = 10,
6060
Positions = polylineLocations.ToList(),
6161
};
6262
}

src/Tracked/Screens/Trails/CreateTrailScreenViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public void AddPin(double latitude, double longitude) {
8181
if (lastLatLng != null) {
8282
CreatePolyline(new MapPolyline {
8383
Positions = new List<ILatLng>() { lastLatLng, thisLatLng },
84-
Colour = Color.Red,
85-
Width = 10,
84+
StrokeColor = Color.Red,
85+
StrokeWidth = 10,
8686
});
8787
} else {
8888
DisplayText = "Now tap to set the next point";
@@ -100,9 +100,9 @@ protected override IEnumerable<MapPin> GetPins() {
100100
protected override IEnumerable<MapPolyline> GetPolylines() {
101101
if (Ride != null) {
102102
yield return new MapPolyline {
103-
Colour = Color.Blue,
103+
StrokeColor = Color.Blue,
104104
Positions = Ride.Locations,
105-
Width = 10,
105+
StrokeWidth = 10,
106106
};
107107
}
108108
}

src/Tracked/Screens/Trails/TrailScreenViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Shared.Dtos;
45
using Shared.Interfaces;
@@ -75,9 +76,9 @@ protected override IEnumerable<MapPin> GetPins() {
7576

7677
protected override IEnumerable<MapPolyline> GetPolylines() {
7778
yield return new MapPolyline {
78-
Colour = Color.Blue,
79-
Width = 10,
80-
Positions = Trail.Locations,
79+
StrokeColor = Color.Blue,
80+
StrokeWidth = 10,
81+
Positions = Trail.Locations.ToList(),
8182
};
8283
}
8384
}

0 commit comments

Comments
 (0)