Skip to content

Commit eb8f7e5

Browse files
committed
- fix issue of converting MapboxGeoJSON Point to GeoJSON.NET.Position
1 parent 8290184 commit eb8f7e5

15 files changed

+179
-69
lines changed

src/libs/Mapbox.Maui/IMapboxView.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public interface IMapFeatureQueryable
6161
Task<IEnumerable<QueriedRenderedFeature>> QueryRenderedFeaturesWith(ScreenPosition point, RenderedQueryOptions options);
6262
}
6363

64-
public interface IMapController
64+
public interface IMapboxController
6565
{
66+
IPosition GetMapPosition(ScreenPosition position);
6667
CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions);
6768
}
6869

src/libs/Mapbox.Maui/MapboxView.cs

+1
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,5 @@ public MapboxStyle MapboxStyle
221221

222222
public IAnnotationController AnnotationController { get; internal set; }
223223
public IMapFeatureQueryable QueryManager { get; internal set; }
224+
public IMapboxController MapboxController { get; internal set; }
224225
}

src/libs/Mapbox.Maui/Platforms/Android/Annotations/PointAnnotationManager.cs

-16
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ public IconRotationAlignment? IconRotationAlignment
5050
get => nativeManager.IconRotationAlignment?.GetValue();
5151
set => nativeManager.IconRotationAlignment = value?.ToPlatform();
5252
}
53-
public IconTextFit? IconTextFit
54-
{
55-
get => nativeManager.IconTextFit?.GetValue();
56-
set => nativeManager.IconTextFit = value?.ToPlatform();
57-
}
58-
public double[] IconTextFitPadding
59-
{
60-
get => nativeManager.IconTextFitPadding.GetValue();
61-
set => nativeManager.IconTextFitPadding = value?.ToPlatform();
62-
}
6353
public bool? SymbolAvoidEdges
6454
{
6555
get => nativeManager.SymbolAvoidEdges?.BooleanValue();
@@ -163,12 +153,6 @@ public TextTranslateAnchor? TextTranslateAnchor
163153
get => nativeManager.TextTranslateAnchor?.GetValue();
164154
set => nativeManager.TextTranslateAnchor = value?.ToPlatform();
165155
}
166-
public double? TextLineHeight
167-
{
168-
get => nativeManager.TextLineHeight?.DoubleValue();
169-
set => nativeManager.TextLineHeight = value?.ToPlatform();
170-
}
171-
172156
public override void AddAnnotations(params PointAnnotation[] xitems)
173157
{
174158
var items = xitems

src/libs/Mapbox.Maui/Platforms/Android/GeometryExtensions.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,33 @@ public static class GeometryExtensions
88
{
99
internal static Com.Mapbox.Geojson.Point ToGeoPoint(this GeoJSON.Text.Geometry.IPosition xvalue)
1010
{
11-
return Com.Mapbox.Geojson.Point.FromLngLat(
11+
return xvalue.Altitude is not null
12+
? Com.Mapbox.Geojson.Point.FromLngLat(
13+
xvalue.Longitude,
14+
xvalue.Latitude,
15+
xvalue.Altitude.Value)
16+
: Com.Mapbox.Geojson.Point.FromLngLat(
1217
xvalue.Longitude,
1318
xvalue.Latitude);
1419
}
15-
20+
21+
public static IPosition ToMapPosition(this Com.Mapbox.Geojson.Point point)
22+
{
23+
return new MapPosition(
24+
point.Latitude(),
25+
point.Longitude(),
26+
point.Altitude());
27+
}
28+
1629
internal static MapTappedPosition ToMapTappedPosition(this Com.Mapbox.Geojson.Point point, ScreenCoordinate screenCoordinate)
1730
{
1831
return new MapTappedPosition(
1932
new ScreenPosition(
2033
screenCoordinate.GetX().PixelToPoint(),
2134
screenCoordinate.GetY().PixelToPoint()),
2235
new MapPosition(
23-
screenCoordinate.GetX().PixelToPoint(),
24-
screenCoordinate.GetY().PixelToPoint(),
36+
point.Latitude(),
37+
point.Longitude(),
2538
point.HasAltitude ? point.Altitude() : null)
2639
);
2740
}
@@ -81,6 +94,11 @@ internal static Com.Mapbox.Geojson.Point ToNative(this Point xvalue)
8194
return Com.Mapbox.Geojson.Point.FromLngLat(xvalue.Y, xvalue.X);
8295
}
8396

97+
internal static Com.Mapbox.Maps.ScreenCoordinate ToScreenCoordinate(this Point xvalue)
98+
{
99+
return new Com.Mapbox.Maps.ScreenCoordinate(xvalue.Y, xvalue.X);
100+
}
101+
84102
internal static GeoJSON.Text.Feature.Feature ToX(this Com.Mapbox.Geojson.Feature src)
85103
=> new GeoJSON.Text.Feature.Feature(
86104
src.Geometry().ToX(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+

2+
namespace MapboxMaui;
3+
4+
partial class MapboxViewHandler : IMapboxController
5+
{
6+
public CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions)
7+
{
8+
var mapView = mapboxFragment?.MapView;
9+
10+
if (mapView == null) return null;
11+
12+
var xcameraOptions = cameraOptions.ToNative();
13+
var xbounds = mapView.MapboxMap.CoordinateBoundsForCamera(xcameraOptions);
14+
15+
return new CoordinateBounds(
16+
xbounds.Southwest.ToMapPosition(),
17+
xbounds.Northeast.ToMapPosition(),
18+
xbounds.InfiniteBounds
19+
);
20+
}
21+
22+
public IPosition GetMapPosition(ScreenPosition position)
23+
{
24+
var mapView = mapboxFragment?.MapView;
25+
26+
if (mapView == null) return null;
27+
28+
var coords = mapView.MapboxMap.CoordinateForPixel(position.ToScreenCoordinate());
29+
return coords.ToMapPosition();
30+
}
31+
}

src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ protected override void ConnectHandler(PlatformView platformView)
229229
{
230230
mapboxView.AnnotationController = this;
231231
mapboxView.QueryManager = this;
232+
mapboxView.MapboxController = this;
232233
}
233234
}
234235

src/libs/Mapbox.Maui/Platforms/iOS/GeometryExtensions.cs

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ internal static CGPoint ToCGPoint(this IPosition xobj)
9393
);
9494
}
9595

96+
internal static CGPoint ToCGPoint(this ScreenPosition xobj)
97+
{
98+
return new CGPoint(
99+
xobj.X,
100+
xobj.Y
101+
);
102+
}
103+
104+
public static IPosition ToMapPosition(this CLLocationCoordinate2D point)
105+
{
106+
return new MapPosition(
107+
point.Latitude,
108+
point.Longitude);
109+
}
110+
96111
internal static NSValue ToNSValue(this IPosition xobj)
97112
{
98113
return NSValue.FromCGPoint(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using MapboxMapsObjC;
2+
3+
namespace MapboxMaui;
4+
5+
partial class MapboxViewHandler : IMapboxController
6+
{
7+
public CoordinateBounds GetCoordinateBoundsForCamera(CameraOptions cameraOptions)
8+
{
9+
var mapView = PlatformView.MapView;
10+
11+
if (mapView == null) return null;
12+
13+
var xcameraOptions = cameraOptions.ToNative();
14+
var xbounds = mapView.MapboxMap().CoordinateBoundsForCameraBounds(xcameraOptions);
15+
16+
return new CoordinateBounds(
17+
xbounds.Southeast.ToMapPosition(),
18+
xbounds.Northeast.ToMapPosition(),
19+
xbounds.InfiniteBounds
20+
);
21+
}
22+
public IPosition GetMapPosition(ScreenPosition position)
23+
{
24+
var mapView = PlatformView.MapView;
25+
26+
if (mapView == null) return null;
27+
28+
var coords = mapView.MapboxMap().CoordinateFor(
29+
position.ToCGPoint()
30+
);
31+
32+
return coords.ToMapPosition();
33+
}
34+
}

src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public partial class MapboxViewHandler
1414
UITapGestureRecognizer mapTapGestureRecognizer;
1515
UILongPressGestureRecognizer mapLongPressGestureRecognizer;
1616

17-
1817
private static void HandleLightChanged(MapboxViewHandler handler, IMapboxView view)
1918
{
2019
var mapView = handler.PlatformView.MapView;
@@ -287,6 +286,7 @@ protected override void DisconnectHandler(PlatformView platformView)
287286
{
288287
mapboxView.AnnotationController = null;
289288
mapboxView.QueryManager = null;
289+
mapboxView.MapboxController = null;
290290
}
291291

292292
var mapView = platformView.MapView;
@@ -312,6 +312,7 @@ protected override void ConnectHandler(PlatformView platformView)
312312
mapboxView.InvokeMapReady();
313313
mapboxView.AnnotationController = this;
314314
mapboxView.QueryManager = this;
315+
mapboxView.MapboxController = this;
315316
}
316317

317318
var mapView = platformView.MapView;

src/qs/MapboxMauiQs/Examples/07.AnimatedMarker/AnimatedMarkerExample.cs

+42-42
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ namespace MapboxMauiQs;
22

33
public class AnimatedMarkerExample : ContentPage, IExamplePage, IQueryAttributable
44
{
5-
static class Constants
5+
static class Constants
66
{
77
public const string markerIconId = "marker_icon";
88
public const string sourceId = "source-id";
99
public const double animationDuration = 2;
10-
}
10+
}
1111

1212
MapboxView map;
1313
IExampleInfo info;
@@ -22,30 +22,30 @@ public AnimatedMarkerExample()
2222
map.MapReady += Map_MapReady;
2323
map.MapLoaded += Map_MapLoaded;
2424
map.Command = new Command<MapTappedPosition>(HandleMapTapped);
25-
}
26-
25+
}
26+
2727
public void ApplyQueryAttributes(IDictionary<string, object> query)
2828
{
2929
info = query["example"] as IExampleInfo;
3030

3131
Title = info?.Title;
32-
}
33-
34-
private void HandleMapTapped(MapTappedPosition point)
35-
{
36-
// Create a GeoJSON data source.
37-
var feature = new Feature(
38-
point.Point
39-
);
40-
var source = new GeoJSONSource(Constants.sourceId)
41-
{
42-
Data = new RawGeoJSONObject(
43-
JsonSerializer.Serialize(feature)
44-
)
45-
};
46-
47-
map.Sources = new[] { source };
48-
}
32+
}
33+
34+
private void HandleMapTapped(MapTappedPosition point)
35+
{
36+
// Create a GeoJSON data source.
37+
var feature = new Feature(
38+
new GeoJSON.Text.Geometry.Point(point.MapPosition)
39+
);
40+
var source = new GeoJSONSource(Constants.sourceId)
41+
{
42+
Data = new RawGeoJSONObject(
43+
JsonSerializer.Serialize(feature)
44+
)
45+
};
46+
47+
map.Sources = new[] { source };
48+
}
4949

5050
private void Map_MapReady(object sender, EventArgs e)
5151
{
@@ -63,27 +63,27 @@ private void Map_MapLoaded(object sender, EventArgs e)
6363
{
6464
var image = new ResolvedImage(Constants.markerIconId, "red_marker");
6565
map.Images = new[] { image };
66-
67-
// Create a GeoJSON data source.
68-
var feature = new Feature(
69-
new GeoJSON.Text.Geometry.Point(currentPosition)
70-
);
71-
var source = new GeoJSONSource(Constants.sourceId)
72-
{
73-
Data = new RawGeoJSONObject(
74-
JsonSerializer.Serialize(feature)
75-
)
76-
};
77-
78-
map.Sources = new[] { source };
79-
80-
// Create a symbol layer
81-
var symbolLayer = new SymbolLayer(id: "layer-id");
82-
symbolLayer.Source = Constants.sourceId;
83-
symbolLayer.IconImage = image;
84-
symbolLayer.IconIgnorePlacement = true;
85-
symbolLayer.IconAllowOverlap = true;
86-
66+
67+
// Create a GeoJSON data source.
68+
var feature = new Feature(
69+
new GeoJSON.Text.Geometry.Point(currentPosition)
70+
);
71+
var source = new GeoJSONSource(Constants.sourceId)
72+
{
73+
Data = new RawGeoJSONObject(
74+
JsonSerializer.Serialize(feature)
75+
)
76+
};
77+
78+
map.Sources = new[] { source };
79+
80+
// Create a symbol layer
81+
var symbolLayer = new SymbolLayer(id: "layer-id");
82+
symbolLayer.Source = Constants.sourceId;
83+
symbolLayer.IconImage = image;
84+
symbolLayer.IconIgnorePlacement = true;
85+
symbolLayer.IconAllowOverlap = true;
86+
8787
map.Layers = new[] { symbolLayer };
8888
}
8989
}

src/qs/MapboxMauiQs/Examples/Annotations/58.AnimatePointAnnotation/AnimatePointAnnotationExample.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ private void Map_MapReady(object sender, EventArgs e)
4343

4444
private void Map_MapLoaded(object sender, EventArgs e)
4545
{
46+
var image = new ResolvedImage("ic_car_top", "ic_car_top");
47+
map.Images = new[] { image };
4648
// Setup Styles, Annotations, etc here
4749
pointAnnotationManager = map.AnnotationController.CreatePointAnnotationManager(
4850
"id", LayerPosition.Unknown()
@@ -51,10 +53,21 @@ private void Map_MapLoaded(object sender, EventArgs e)
5153

5254
var noAnimateItems = new List<PointAnnotation>();
5355
for ( int i = 0; i< noAnimateCarNum; i++) {
54-
//noAnimateItems.Add(new PointAnnotation
55-
//{
56-
57-
//});
56+
var point = RandomizePoint();
57+
noAnimateItems.Add(new PointAnnotation(point, Guid.NewGuid().ToString()) {
58+
IconImage = "ic_car_top",
59+
});
5860
}
61+
pointAnnotationManager.AddAnnotations(noAnimateItems.ToArray());
62+
}
63+
64+
private GeoJSON.Text.Geometry.Point RandomizePoint()
65+
{
66+
CoordinateBounds bounds = map.MapboxController.GetCoordinateBoundsForCamera(map.CameraOptions);
67+
var generator = new Random();
68+
var lat = bounds.Southwest.Latitude + (bounds.Northeast.Latitude - bounds.Southwest.Latitude) * generator.NextDouble();
69+
var lon = bounds.Southwest.Longitude + (bounds.Northeast.Longitude - bounds.Southwest.Longitude) * generator.NextDouble();
70+
var position = new MapPosition(lat, lon);
71+
return new GeoJSON.Text.Geometry.Point(position);
5972
}
6073
}

src/qs/MapboxMauiQs/Examples/Annotations/58.AnimatePointAnnotation/AnimatePointAnnotationExampleInfo.cs

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ class AnimatePointAnnotationExampleInfo : IExampleInfo
66
public string Title => "Animate Point Annotation";
77
public string Subtitle => "Animate PointAnnotations on a map";
88
public string PageRoute => typeof(AnimatePointAnnotationExample).FullName;
9+
10+
public int GroupIndex => 3;
11+
public int Index => 58;
912
}

src/qs/MapboxMauiQs/MapboxMauiQs.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<Compile Remove="**\**\*.iOS.cs" />
8181
</ItemGroup>
8282
<ItemGroup>
83-
<None Remove="Resources\Images\ic_car_top.png" />
83+
<None Remove="Resources\Images\ic_car_top.svg" />
8484
<None Remove="Resources\Images\ic_clear.png" />
8585
<None Remove="Resources\Images\ic_taxi_top.png" />
8686
</ItemGroup>
Binary file not shown.
Loading

0 commit comments

Comments
 (0)