Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit bb624f6

Browse files
committed
Add UpdateViewPortAction
1 parent 9ac3db1 commit bb624f6

File tree

8 files changed

+117
-11
lines changed

8 files changed

+117
-11
lines changed

Naxam.Mapbox.Forms/MapView.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,28 @@ public Func<string, bool, bool, bool> UpdateLayerFunc {
340340
SetValue (UpdateLayerFuncProperty, value);
341341
}
342342
}
343+
344+
/// <summary>
345+
/// Update view port (camera)
346+
/// Params: Center location, zoom level, bearing, animated, completion handler
347+
/// </summary>
348+
public static readonly BindableProperty UpdateViewPortActionProperty = BindableProperty.Create(
349+
nameof(UpdateViewPortAction),
350+
typeof(Action<Position, double?, double?, bool, Action>),
351+
typeof(MapView),
352+
default(Action<Position, double?, double?, bool, Action>),
353+
BindingMode.OneWayToSource);
354+
355+
public Action<Position, double?, double?, bool, Action> UpdateViewPortAction
356+
{
357+
get
358+
{
359+
return ((Action<Position, double?, double?, bool, Action>)GetValue(UpdateViewPortActionProperty));
360+
}
361+
set
362+
{
363+
SetValue(UpdateViewPortActionProperty, value);
364+
}
365+
}
343366
}
344367
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using static Com.Mapbox.Mapboxsdk.Maps.MapboxMap;
3+
4+
namespace Naxam.Mapbox.Platform.Droid
5+
{
6+
public class CancelableCallback: Java.Lang.Object, ICancelableCallback
7+
{
8+
public Action FinishHandler;
9+
public Action CancelHandler;
10+
11+
public CancelableCallback()
12+
{
13+
}
14+
15+
public void OnCancel()
16+
{
17+
CancelHandler?.Invoke();
18+
}
19+
20+
public void OnFinish()
21+
{
22+
FinishHandler?.Invoke();
23+
}
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Com.Mapbox.Mapboxsdk.Geometry;
3+
using Naxam.Controls.Forms;
4+
5+
namespace Naxam.Controls.Platform.Droid
6+
{
7+
public static class PositionExtensions
8+
{
9+
public static LatLng ToLatLng(this Position pos) {
10+
return new LatLng(pos.Lat, pos.Long);
11+
}
12+
}
13+
}

Naxam.Mapbox.Platform.Droid/MapViewRenderer.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ public void SetupFunctions()
157157
//https://github.com/mapbox/mapbox-gl-native/issues/9511
158158
map.SetStyleUrl(map.StyleUrl, null);
159159
});
160+
161+
Element.UpdateViewPortAction = (Position centerLocation, double? zoomLevel, double? bearing, bool animated, Action completionHandler) => {
162+
var newPosition = new CameraPosition.Builder()
163+
.Bearing(bearing ?? map.CameraPosition.Bearing)
164+
.Target(centerLocation?.ToLatLng() ?? map.CameraPosition.Target)
165+
.Zoom(zoomLevel ?? map.CameraPosition.Zoom)
166+
.Build();
167+
var callback = completionHandler == null ? null : new CancelableCallback()
168+
{
169+
FinishHandler = completionHandler,
170+
CancelHandler = completionHandler
171+
};
172+
var update = CameraUpdateFactory.NewCameraPosition(newPosition);
173+
if (animated) {
174+
map.AnimateCamera(update, callback);
175+
}
176+
else {
177+
map.MoveCamera(update, callback);
178+
}
179+
};
160180
}
161181

162182
byte[] TakeMapSnapshot()
@@ -222,7 +242,7 @@ protected override void OnElementPropertyChanged(object sender, System.Component
222242
if (!ReferenceEquals(Element.Center, currentCamera))
223243
{
224244
if (Element.Center == null) return;
225-
FocustoLocation(new LatLng(Element.Center.Lat, Element.Center.Long));
245+
FocustoLocation(Element.Center.ToLatLng());
226246
}
227247
}
228248
else if (e.PropertyName == MapView.MapStyleProperty.PropertyName && map != null)
@@ -557,8 +577,7 @@ private Sdk.Annotations.Annotation AddAnnotation(Annotation at)
557577
var marker = new MarkerOptions();
558578
marker.SetTitle(at.Title);
559579
marker.SetSnippet(at.Title);
560-
marker.SetPosition(new LatLng(((PointAnnotation)at).Coordinate.Lat,
561-
((PointAnnotation)at).Coordinate.Long));
580+
marker.SetPosition(((PointAnnotation)at).Coordinate.ToLatLng());
562581
options = map.AddMarker(marker);
563582
}
564583
else if (at is PolylineAnnotation)
@@ -578,14 +597,14 @@ private Sdk.Annotations.Annotation AddAnnotation(Annotation at)
578597
if (_annotationDictionaries.ContainsKey(at.Id))
579598
{
580599
var poly = _annotationDictionaries[at.Id] as Polyline;
581-
poly.AddPoint(new LatLng(polyline.Coordinates.ElementAt(e.NewStartingIndex).Lat, polyline.Coordinates.ElementAt(e.NewStartingIndex).Long));
600+
poly.AddPoint(polyline.Coordinates.ElementAt(e.NewStartingIndex).ToLatLng());
582601
}
583602
else
584603
{
585604
var coords = new ArrayList();
586605
for (var i = 0; i < polyline.Coordinates.Count(); i++)
587606
{
588-
coords.Add(new LatLng(polyline.Coordinates.ElementAt(i).Lat, polyline.Coordinates.ElementAt(i).Long));
607+
coords.Add(polyline.Coordinates.ElementAt(i).ToLatLng());
589608
}
590609
var polylineOpt = new PolylineOptions();
591610
polylineOpt.Polyline.Width = Context.ToPixels(1);
@@ -600,7 +619,7 @@ private Sdk.Annotations.Annotation AddAnnotation(Annotation at)
600619
if (_annotationDictionaries.ContainsKey(at.Id))
601620
{
602621
var poly = _annotationDictionaries[at.Id] as Polyline;
603-
poly.Points.Remove(new LatLng(polyline.Coordinates.ElementAt(e.OldStartingIndex).Lat, polyline.Coordinates.ElementAt(e.OldStartingIndex).Long));
622+
poly.Points.Remove(polyline.Coordinates.ElementAt(e.OldStartingIndex).ToLatLng());
604623
}
605624
}
606625
};
@@ -667,7 +686,7 @@ public void OnMapReady(MapboxMap p0)
667686

668687
if (Element.Center != null) {
669688
map.CameraPosition = new CameraPosition.Builder()
670-
.Target(new LatLng(Element.Center.Lat, Element.Center.Long))
689+
.Target(Element.Center.ToLatLng())
671690
.Zoom(Element.ZoomLevel)
672691
.Build();
673692
}

Naxam.Mapbox.Platform.Droid/Naxam.Mapbox.Platform.Droid.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168
<Compile Include="Extensions\LayerExtensions.cs" />
169169
<Compile Include="Extensions\AnnotationExtensions.cs" />
170170
<Compile Include="Extensions\FeatureExtensions.cs" />
171+
<Compile Include="Extensions\PositionExtensions.cs" />
172+
<Compile Include="CancelableCallback.cs" />
171173
</ItemGroup>
172174
<ItemGroup>
173175
<None Include="Resources\AboutResources.txt" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using CoreLocation;
3+
using Naxam.Controls.Forms;
4+
5+
namespace Naxam.Controls.Platform.iOS
6+
{
7+
public static class PositionExtensions
8+
{
9+
public static CLLocationCoordinate2D ToCLCoordinate(this Position pos)
10+
{
11+
return new CLLocationCoordinate2D(pos.Lat, pos.Long);
12+
}
13+
}
14+
}

Naxam.Mapbox.Platform.iOS/MapViewRenderer.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ void SetupFunctions ()
404404
}
405405
return false;
406406
};
407+
408+
Element.UpdateViewPortAction = (Position centerLocation, double? zoomLevel, double? bearing, bool animated, Action completionBlock) => {
409+
MapView.SetCenterCoordinate(
410+
centerLocation?.ToCLCoordinate() ?? MapView.CenterCoordinate,
411+
zoomLevel ?? MapView.ZoomLevel,
412+
bearing ?? MapView.Direction,
413+
animated,
414+
completionBlock
415+
);
416+
};
407417
}
408418

409419
NSSet<NSString> SelectableLayersFromSources (string [] layersId)
@@ -676,8 +686,7 @@ MGLShape ShapeFromAnnotation (FormsMB.Annotation annotation)
676686
MGLShape shape = null;
677687
if (annotation is PointAnnotation) {
678688
shape = new MGLPointAnnotation () {
679-
Coordinate = new CLLocationCoordinate2D (((PointAnnotation)annotation).Coordinate.Lat,
680-
((PointAnnotation)annotation).Coordinate.Long),
689+
Coordinate = ((PointAnnotation)annotation).Coordinate.ToCLCoordinate()
681690
};
682691
} else if (annotation is PolylineAnnotation) {
683692
var polyline = annotation as PolylineAnnotation;
@@ -731,11 +740,11 @@ MGLPolyline PolyLineWithCoordinates (Position[] positions)
731740
if (positions == null || positions.Length == 0) {
732741
return null;
733742
}
734-
var first = new CLLocationCoordinate2D (positions [0].Lat, positions [0].Long);
743+
var first = positions[0].ToCLCoordinate();
735744
var output = MGLPolyline.PolylineWithCoordinates (ref first, 1);
736745
var i = 1;
737746
while (i < positions.Length) {
738-
var coord = new CLLocationCoordinate2D (positions [i].Lat, positions [i].Long);
747+
var coord = positions[i].ToCLCoordinate();
739748
output.AppendCoordinates (ref coord, 1);
740749
i++;
741750
}

Naxam.Mapbox.Platform.iOS/Naxam.Mapbox.Platform.iOS.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="MapViewRenderer.cs" />
107107
<Compile Include="Extensions\MGLShapeExtensions.cs" />
108108
<Compile Include="TypeConverter.cs" />
109+
<Compile Include="Extensions\PositionExtensions.cs" />
109110
</ItemGroup>
110111
<ItemGroup>
111112
<ProjectReference Include="..\Naxam.Mapbox.Forms\Naxam.Mapbox.Forms.csproj">

0 commit comments

Comments
 (0)