Skip to content

Commit

Permalink
fix: allows to change geometry value of an annotation (v11.5.1-alpha04)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyen-vuduc committed Aug 20, 2024
1 parent 79b7551 commit 53f7cd6
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 4 deletions.
2 changes: 2 additions & 0 deletions kill-jd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
taskkill -F -im java.exe
taskkill -F -im dotnet.exe
2 changes: 1 addition & 1 deletion src/libs/Mapbox.Maui/Mapbox.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<RepositoryUrl>https://github.com/tuyen-vuduc/mapbox-maui</RepositoryUrl>
<PackageProjectUrl>https://mapbox.tuyen-vuduc.tech</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageVersion>11.5.1-alpha03</PackageVersion>
<PackageVersion>11.5.1-alpha04</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>tv-mapbox.png</PackageIcon>
Expand Down
7 changes: 5 additions & 2 deletions src/libs/Mapbox.Maui/Models/Annotations/IAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public abstract class Annotation<T>
{
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<string, object> UserInfo { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
103 changes: 103 additions & 0 deletions src/qs/MapboxMauiQs/Examples/Lab/71.MovingCar/MovingCarExample.cs
Original file line number Diff line number Diff line change
@@ -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<string, object> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 53f7cd6

Please sign in to comment.