Skip to content

Commit

Permalink
feat: Sample AnimatePointAnnotation (not done) (#15)
Browse files Browse the repository at this point in the history
* - only pack the MAUI lib

* - Change to IPosition instead of GeoJSON.Point for MapTappedLocation
- Change MapTappedPosition to a record
- Add CoordinateBounds

* - Add empty example AnimatePointAnnotation

* - fix issue of converting MapboxGeoJSON Point to GeoJSON.NET.Position

* bumb new version
  • Loading branch information
tuyen-vuduc authored Jun 26, 2024
1 parent 55f833a commit d79f6e9
Show file tree
Hide file tree
Showing 40 changed files with 331 additions and 142 deletions.
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dotnet nuget locals -c all
dotnet build -t:Clean,Rebuild src/qs/MapboxMauiQs/MapboxMauiQs.csproj \
-property:MAPBOX_DOWNLOADS_TOKEN=$MAPBOX_DOWNLOADS_TOKEN

dotnet pack -c Release -t:Clean,Rebuild mapbox-maui.sln --output $PWD/nugets
dotnet pack -c Release -t:Clean,Rebuild src/libs/Mapbox.Maui/Mapbox.Maui.csproj \
--output $PWD/nugets
6 changes: 6 additions & 0 deletions src/libs/Mapbox.Maui/IMapboxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public interface IMapFeatureQueryable
Task<IEnumerable<QueriedRenderedFeature>> QueryRenderedFeaturesWith(ScreenPosition point, RenderedQueryOptions options);
}

public interface IMapboxController
{
IPosition GetMapPosition(ScreenPosition position);
CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions);
}

public class MapTappedEventArgs : EventArgs
{
public MapTappedPosition Position { get; }
Expand Down
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 @@ -47,7 +47,7 @@
<RepositoryUrl>https://github.com/tuyen-vuduc/mapbox-maui</RepositoryUrl>
<PackageProjectUrl>https://mapbox.tuyen-vuduc.tech</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageVersion>11.3.0-alpha02</PackageVersion>
<PackageVersion>11.3.0-alpha03</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>tv-mapbox.png</PackageIcon>
Expand Down
10 changes: 1 addition & 9 deletions src/libs/Mapbox.Maui/MapboxView.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,4 @@ public ICommand MapLoadedCommand
get => (ICommand)GetValue(MapLoadedCommandProperty);
set => SetValue(MapLoadedCommandProperty, value);
}
}

public class MapTappedPosition
{
public ScreenPosition ScreenPosition { get; set; }

public GeoJSON.Text.Geometry.Point Point { get; set; }
}

}
1 change: 1 addition & 0 deletions src/libs/Mapbox.Maui/MapboxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,5 @@ public MapboxStyle MapboxStyle

public IAnnotationController AnnotationController { get; internal set; }
public IMapFeatureQueryable QueryManager { get; internal set; }
public IMapboxController MapboxController { get; internal set; }
}
5 changes: 1 addition & 4 deletions src/libs/Mapbox.Maui/Models/BaseKVContainer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace MapboxMaui;
namespace MapboxMaui;

public abstract class BaseKVContainer : INotifyCollectionChanged
{
Expand Down
6 changes: 6 additions & 0 deletions src/libs/Mapbox.Maui/Models/CoordinateBounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MapboxMaui;

public record CoordinateBounds (
IPosition Southwest,
IPosition Northeast,
bool InfiniteBounds = false);
5 changes: 5 additions & 0 deletions src/libs/Mapbox.Maui/Models/MapTappedPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace MapboxMaui;

public record MapTappedPosition(
ScreenPosition ScreenPosition,
IPosition MapPosition);
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ public IconRotationAlignment? IconRotationAlignment
get => nativeManager.IconRotationAlignment?.GetValue();
set => nativeManager.IconRotationAlignment = value?.ToPlatform();
}
public IconTextFit? IconTextFit
{
get => nativeManager.IconTextFit?.GetValue();
set => nativeManager.IconTextFit = value?.ToPlatform();
}
public double[] IconTextFitPadding
{
get => nativeManager.IconTextFitPadding.GetValue();
set => nativeManager.IconTextFitPadding = value?.ToPlatform();
}
public bool? SymbolAvoidEdges
{
get => nativeManager.SymbolAvoidEdges?.BooleanValue();
Expand Down Expand Up @@ -163,12 +153,6 @@ public TextTranslateAnchor? TextTranslateAnchor
get => nativeManager.TextTranslateAnchor?.GetValue();
set => nativeManager.TextTranslateAnchor = value?.ToPlatform();
}
public double? TextLineHeight
{
get => nativeManager.TextLineHeight?.DoubleValue();
set => nativeManager.TextLineHeight = value?.ToPlatform();
}

public override void AddAnnotations(params PointAnnotation[] xitems)
{
var items = xitems
Expand Down
41 changes: 28 additions & 13 deletions src/libs/Mapbox.Maui/Platforms/Android/GeometryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@ public static class GeometryExtensions
{
internal static Com.Mapbox.Geojson.Point ToGeoPoint(this GeoJSON.Text.Geometry.IPosition xvalue)
{
return Com.Mapbox.Geojson.Point.FromLngLat(
return xvalue.Altitude is not null
? Com.Mapbox.Geojson.Point.FromLngLat(
xvalue.Longitude,
xvalue.Latitude,
xvalue.Altitude.Value)
: Com.Mapbox.Geojson.Point.FromLngLat(
xvalue.Longitude,
xvalue.Latitude);
}


public static IPosition ToMapPosition(this Com.Mapbox.Geojson.Point point)
{
return new MapPosition(
point.Latitude(),
point.Longitude(),
point.Altitude());
}

internal static MapTappedPosition ToMapTappedPosition(this Com.Mapbox.Geojson.Point point, ScreenCoordinate screenCoordinate)
{
return new MapTappedPosition
{
ScreenPosition = new ScreenPosition(
return new MapTappedPosition(
new ScreenPosition(
screenCoordinate.GetX().PixelToPoint(),
screenCoordinate.GetY().PixelToPoint()),
Point = new GeoJSON.Text.Geometry.Point(
new Position(
screenCoordinate.GetX().PixelToPoint(),
screenCoordinate.GetY().PixelToPoint(),
point.HasAltitude ? point.Altitude() : null)
)
};
new MapPosition(
point.Latitude(),
point.Longitude(),
point.HasAltitude ? point.Altitude() : null)
);
}

internal static Com.Mapbox.Geojson.IGeometry ToNative(this GeoJSON.Text.Geometry.IGeometryObject xvalue)
Expand Down Expand Up @@ -84,6 +94,11 @@ internal static Com.Mapbox.Geojson.Point ToNative(this Point xvalue)
return Com.Mapbox.Geojson.Point.FromLngLat(xvalue.Y, xvalue.X);
}

internal static Com.Mapbox.Maps.ScreenCoordinate ToScreenCoordinate(this Point xvalue)
{
return new Com.Mapbox.Maps.ScreenCoordinate(xvalue.Y, xvalue.X);
}

internal static GeoJSON.Text.Feature.Feature ToX(this Com.Mapbox.Geojson.Feature src)
=> new GeoJSON.Text.Feature.Feature(
src.Geometry().ToX(),
Expand All @@ -96,7 +111,7 @@ internal static IGeometryObject ToX(this Com.Mapbox.Geojson.IGeometry src)
{
case Com.Mapbox.Geojson.Point point:
return new GeoJSON.Text.Geometry.Point(
new Position(
new MapPosition(
point.Latitude(), point.Longitude(),
point.HasAltitude ? point.Altitude() : null
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

namespace MapboxMaui;

partial class MapboxViewHandler : IMapboxController
{
public CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions)
{
var mapView = mapboxFragment?.MapView;

if (mapView == null) return null;

var xcameraOptions = cameraOptions.ToNative();
var xbounds = mapView.MapboxMap.CoordinateBoundsForCamera(xcameraOptions);

return new CoordinateBounds(
xbounds.Southwest.ToMapPosition(),
xbounds.Northeast.ToMapPosition(),
xbounds.InfiniteBounds
);
}

public IPosition GetMapPosition(ScreenPosition position)
{
var mapView = mapboxFragment?.MapView;

if (mapView == null) return null;

var coords = mapView.MapboxMap.CoordinateForPixel(position.ToScreenCoordinate());
return coords.ToMapPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected override void ConnectHandler(PlatformView platformView)
{
mapboxView.AnnotationController = this;
mapboxView.QueryManager = this;
mapboxView.MapboxController = this;
}
}

Expand Down
33 changes: 22 additions & 11 deletions src/libs/Mapbox.Maui/Platforms/iOS/GeometryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ public static class GeometryExtensions
{
internal static MapTappedPosition ToMapTappedPosition(this CLLocationCoordinate2D coords, CGPoint screenCoordinate)
{
return new MapTappedPosition
{
ScreenPosition = new ScreenPosition(
return new MapTappedPosition(
new ScreenPosition(
screenCoordinate.X,
screenCoordinate.Y
),
Point = new GeoJSON.Text.Geometry.Point(
new Position(
coords.Latitude,
coords.Longitude
)
),
};
new MapPosition(
coords.Latitude,
coords.Longitude
));
}

internal static MBXGeometry ToNative(this IGeometryObject xobj)
Expand Down Expand Up @@ -97,6 +93,21 @@ internal static CGPoint ToCGPoint(this IPosition xobj)
);
}

internal static CGPoint ToCGPoint(this ScreenPosition xobj)
{
return new CGPoint(
xobj.X,
xobj.Y
);
}

public static IPosition ToMapPosition(this CLLocationCoordinate2D point)
{
return new MapPosition(
point.Latitude,
point.Longitude);
}

internal static NSValue ToNSValue(this IPosition xobj)
{
return NSValue.FromCGPoint(
Expand All @@ -105,7 +116,7 @@ internal static NSValue ToNSValue(this IPosition xobj)
}

internal static IPosition ToPosition(this NSValue src)
=> new Position(
=> new MapPosition(
src.CoordinateValue.Latitude,
src.CoordinateValue.Longitude
);
Expand Down
34 changes: 34 additions & 0 deletions src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.Controller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MapboxMapsObjC;

namespace MapboxMaui;

partial class MapboxViewHandler : IMapboxController
{
public CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions)
{
var mapView = PlatformView.MapView;

if (mapView == null) return null;

var xcameraOptions = cameraOptions.ToNative();
var xbounds = mapView.MapboxMap().CoordinateBoundsForCameraBounds(xcameraOptions);

return new CoordinateBounds(
xbounds.Southeast.ToMapPosition(),
xbounds.Northeast.ToMapPosition(),
xbounds.InfiniteBounds
);
}
public IPosition GetMapPosition(ScreenPosition position)
{
var mapView = PlatformView.MapView;

if (mapView == null) return null;

var coords = mapView.MapboxMap().CoordinateFor(
position.ToCGPoint()
);

return coords.ToMapPosition();
}
}
3 changes: 2 additions & 1 deletion src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public partial class MapboxViewHandler
UITapGestureRecognizer mapTapGestureRecognizer;
UILongPressGestureRecognizer mapLongPressGestureRecognizer;


private static void HandleLightChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.PlatformView.MapView;
Expand Down Expand Up @@ -287,6 +286,7 @@ protected override void DisconnectHandler(PlatformView platformView)
{
mapboxView.AnnotationController = null;
mapboxView.QueryManager = null;
mapboxView.MapboxController = null;
}

var mapView = platformView.MapView;
Expand All @@ -312,6 +312,7 @@ protected override void ConnectHandler(PlatformView platformView)
mapboxView.InvokeMapReady();
mapboxView.AnnotationController = this;
mapboxView.QueryManager = this;
mapboxView.MapboxController = this;
}

var mapView = platformView.MapView;
Expand Down
5 changes: 4 additions & 1 deletion src/libs/Mapbox.Maui/usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
global using System.Text.Json;
global using System.Collections.ObjectModel;
global using System.Collections.Specialized;
global using GeoJSON.Text;
global using GeoJSON.Text.Feature;
global using GeoJSON.Text.Geometry;
Expand All @@ -8,4 +10,5 @@
global using MapboxMaui.Offline;
global using MapboxMaui.Annotations;
global using iOSPage = Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.Page;
global using ScreenPosition = Microsoft.Maui.Graphics.Point;
global using ScreenPosition = Microsoft.Maui.Graphics.Point;
global using MapPosition = GeoJSON.Text.Geometry.Position;
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)

private void Map_MapReady(object sender, EventArgs e)
{
var centerLocation = new Position(55.70651, 12.554729);
var centerLocation = new MapPosition(55.70651, 12.554729);
var cameraOptions = new CameraOptions
{
Center = centerLocation,
Expand All @@ -56,7 +56,7 @@ private void Map_MapLoaded(object sender, EventArgs e)

var feature = new Feature(
new GeoJSON.Text.Geometry.Point(
new GeoJSON.Text.Geometry.Position(latitude: 55.608166, longitude: 12.65147)
new MapPosition(latitude: 55.608166, longitude: 12.65147)
),
new Dictionary<string, object> {
{ Constants.ICON_KEY, Constants.BLUE_MARKER_PROPERTY }
Expand All @@ -66,7 +66,7 @@ private void Map_MapLoaded(object sender, EventArgs e)

var feature1 = new Feature(
new GeoJSON.Text.Geometry.Point(
new GeoJSON.Text.Geometry.Position(latitude: 55.70651, longitude: 12.554729)
new MapPosition(latitude: 55.70651, longitude: 12.554729)
),
new Dictionary<string, object> {
{ Constants.ICON_KEY, Constants.RED_MARKER_PROPERTY }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void Map_StyleLoaded(object sender, EventArgs e)
var source = new GeoJSONSource(sourceId)
{
Data = new GeoJSON.Text.Geometry.Point(
new Position(55.665957, 12.550343))
new MapPosition(55.665957, 12.550343))
};

map.Sources = new[] { source };
Expand All @@ -43,7 +43,7 @@ private void Map_StyleLoaded(object sender, EventArgs e)
private void Map_MapReady(object sender, EventArgs e)
{
// Do any additional setup after loading the view.
var center = new Position(55.665957, 12.550343);
var center = new MapPosition(55.665957, 12.550343);
var cameraOptions = new CameraOptions {
Center = center,
Zoom = 8,
Expand Down
Loading

0 comments on commit d79f6e9

Please sign in to comment.