From 53f7cd6039b68a43d9e889027d749f24b6c3e642 Mon Sep 17 00:00:00 2001 From: Tuyen Vu Date: Tue, 20 Aug 2024 07:21:31 +0700 Subject: [PATCH] fix: allows to change geometry value of an annotation (v11.5.1-alpha04) --- kill-jd.sh | 2 + src/libs/Mapbox.Maui/Mapbox.Maui.csproj | 2 +- .../Models/Annotations/IAnnotation.cs | 7 +- .../Android/Annotations/AnnotationManager.cs | 3 +- .../Annotations/CircleAnnotationManager.cs | 3 + .../Annotations/PointAnnotationManager.cs | 2 + .../Annotations/PolygonAnnotationManager.cs | 2 + .../Annotations/PolylineAnnotationManager.cs | 2 + .../Lab/71.MovingCar/MovingCarExample.cs | 103 ++++++++++++++++++ .../Lab/71.MovingCar/MovingCarExampleInfo.cs | 10 ++ 10 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 kill-jd.sh create mode 100644 src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExample.cs create mode 100644 src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExampleInfo.cs diff --git a/kill-jd.sh b/kill-jd.sh new file mode 100644 index 0000000..42c72dd --- /dev/null +++ b/kill-jd.sh @@ -0,0 +1,2 @@ +taskkill -F -im java.exe +taskkill -F -im dotnet.exe \ No newline at end of file diff --git a/src/libs/Mapbox.Maui/Mapbox.Maui.csproj b/src/libs/Mapbox.Maui/Mapbox.Maui.csproj index cba716f..6e4c2e3 100644 --- a/src/libs/Mapbox.Maui/Mapbox.Maui.csproj +++ b/src/libs/Mapbox.Maui/Mapbox.Maui.csproj @@ -61,7 +61,7 @@ https://github.com/tuyen-vuduc/mapbox-maui https://mapbox.tuyen-vuduc.tech false - 11.5.1-alpha03 + 11.5.1-alpha04 README.md LICENSE tv-mapbox.png diff --git a/src/libs/Mapbox.Maui/Models/Annotations/IAnnotation.cs b/src/libs/Mapbox.Maui/Models/Annotations/IAnnotation.cs index 4b5971d..67a185d 100644 --- a/src/libs/Mapbox.Maui/Models/Annotations/IAnnotation.cs +++ b/src/libs/Mapbox.Maui/Models/Annotations/IAnnotation.cs @@ -20,9 +20,12 @@ public abstract class Annotation { public string Id { get; set; } - public IGeometryObject Geometry { get; } + public IGeometryObject Geometry { get; private set; } - public T GeometryValue => (T)Geometry; + public T GeometryValue { + get => (T)Geometry; + set => Geometry = value; + } public IReadOnlyDictionary UserInfo { get; set; } diff --git a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/AnnotationManager.cs b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/AnnotationManager.cs index 6d42113..3a7b1f8 100644 --- a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/AnnotationManager.cs +++ b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/AnnotationManager.cs @@ -45,7 +45,7 @@ public void UpdateAnnotations(params TAnnotation[] annotations) if (annotations.Length == 0) return; var xannotations = annotations - .Select(ToPlatformAnnotationOption) + .Select(ToPlatformAnnotation) .ToList(); NativeManager.Update(xannotations); @@ -60,5 +60,6 @@ public void RemoveAnnotations(params string[] annotationIDs) } protected abstract IAnnotationOptions ToPlatformAnnotationOption(TAnnotation annotation); + protected abstract Annotation ToPlatformAnnotation(TAnnotation annotation); protected abstract IList GetNativeAnnotations(params string[] annotationIDs); } diff --git a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/CircleAnnotationManager.cs b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/CircleAnnotationManager.cs index e76a438..d8ff8d8 100644 --- a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/CircleAnnotationManager.cs +++ b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/CircleAnnotationManager.cs @@ -1,5 +1,6 @@ namespace MapboxMaui.Annotations; +using Android.Graphics; using Com.Mapbox.Maps.Plugins.Annotations; using System.Collections; using PlatformCircleAnnotationManager = Com.Mapbox.Maps.Plugins.Annotations.Generated.CircleAnnotationManager; @@ -61,5 +62,7 @@ protected override IList GetNativeAnnotations(params string[] annotationIDs) } return itemsToDelete; } + protected override Annotation ToPlatformAnnotation(CircleAnnotation annotation) + => (Annotation)annotation.ToPlatformValue().Build(annotation.Id, nativeManager); } diff --git a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PointAnnotationManager.cs b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PointAnnotationManager.cs index 2e90b6e..ea2e4a8 100644 --- a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PointAnnotationManager.cs +++ b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PointAnnotationManager.cs @@ -175,5 +175,7 @@ protected override IList GetNativeAnnotations(params string[] annotationIDs) } return itemsToDelete; } + protected override Annotation ToPlatformAnnotation(PointAnnotation annotation) + => (Annotation)annotation.ToPlatformValue().Build(annotation.Id, nativeManager); } diff --git a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolygonAnnotationManager.cs b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolygonAnnotationManager.cs index cfe09d4..22200ae 100644 --- a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolygonAnnotationManager.cs +++ b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolygonAnnotationManager.cs @@ -56,4 +56,6 @@ protected override IList GetNativeAnnotations(params string[] annotationIDs) } return itemsToDelete; } + protected override Annotation ToPlatformAnnotation(PolygonAnnotation annotation) + => (Annotation)annotation.ToPlatformValue().Build(annotation.Id, nativeManager); } \ No newline at end of file diff --git a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolylineAnnotationManager.cs b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolylineAnnotationManager.cs index 2434483..1e37880 100644 --- a/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolylineAnnotationManager.cs +++ b/src/libs/Mapbox.Maui/Platforms/Android/Annotations/PolylineAnnotationManager.cs @@ -72,4 +72,6 @@ protected override IList GetNativeAnnotations(params string[] annotationIDs) } return itemsToDelete; } + protected override Annotation ToPlatformAnnotation(PolylineAnnotation annotation) + => (Annotation)annotation.ToPlatformValue().Build(annotation.Id, nativeManager); } \ No newline at end of file diff --git a/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExample.cs b/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExample.cs new file mode 100644 index 0000000..6c09943 --- /dev/null +++ b/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExample.cs @@ -0,0 +1,103 @@ +namespace MapboxMauiQs; + +public class MovingCarExample : ContentPage, IExamplePage, IQueryAttributable +{ + MapboxView map; + IExampleInfo info; + IPosition lastPosition; + + public MovingCarExample() + { + iOSPage.SetUseSafeArea(this, false); + Content = new Grid + { + Children = { + (map = new MapboxView()), + new HorizontalStackLayout + { + VerticalOptions = LayoutOptions.End, + HorizontalOptions = LayoutOptions.Center, + Margin = new Thickness(16), + Spacing = 16, + Children = + { + new Button + { + Text = "Move", + Command = new Command(Move), + }, + new Button + { + Text = "Center", + Command = new Command(Centralize), + }, + }, + }, + } + }; + + map.MapReady += Map_MapReady; + map.MapLoaded += Map_MapLoaded; + } + + private void Centralize(object obj) + { + if (carAnnotation is null) return; + + map.CameraController.EaseTo(new CameraOptions() + { + Center = carAnnotation.GeometryValue.Coordinates, + }); + } + + private void Move(object obj) + { + if (carAnnotation is null) return; + + var currentPosition = carAnnotation.GeometryValue; + carAnnotation.GeometryValue = new GeoJSON.Text.Geometry.Point( + new MapPosition( + currentPosition.Latitude() + 0.001, + currentPosition.Longitude() + 0.0001 + ) + ); + pointAnnotationManager.UpdateAnnotations(carAnnotation); + } + + public void ApplyQueryAttributes(IDictionary query) + { + info = query["example"] as IExampleInfo; + + Title = info?.Title; + } + + private void Map_MapReady(object sender, EventArgs e) + { + var centerLocation = lastPosition = new MapPosition(21.0278, 105.8342); + var cameraOptions = new CameraOptions + { + Center = centerLocation, + Zoom = 14, + }; + + map.CameraOptions = cameraOptions; + map.MapboxStyle = MapboxStyle.MAPBOX_STREETS; + } + + IPointAnnotationManager pointAnnotationManager; + PointAnnotation carAnnotation; + private void Map_MapLoaded(object sender, EventArgs e) + { + map.Images = [ + new ResolvedImage("CAR", "ic_car_top"), + ]; + + pointAnnotationManager = map.AnnotationController.CreatePointAnnotationManager("CARS", LayerPosition.Unknown()); + + carAnnotation = new PointAnnotation(new GeoJSON.Text.Geometry.Point(lastPosition)) + { + IconImage = "CAR", + }; + pointAnnotationManager.AddAnnotations(carAnnotation); + } +} \ No newline at end of file diff --git a/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExampleInfo.cs b/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExampleInfo.cs new file mode 100644 index 0000000..03bfb8a --- /dev/null +++ b/src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExampleInfo.cs @@ -0,0 +1,10 @@ +namespace MapboxMauiQs; + +class MovingCarExampleInfo : IExampleInfo +{ + public string Group => "Lab"; + public string Title => "Display a car moving on map"; + public string Subtitle => "Demo how to show a car moving on the map"; + public string PageRoute => typeof(MovingCarExample).FullName; + public int Index => 71; +} \ No newline at end of file