Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Set user location anchor. #531

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ static void interpretMapLibreMapOptions(Object o, MapLibreMapOptionsSink sink, C
if (myLocationTrackingMode != null) {
sink.setMyLocationTrackingMode(toInt(myLocationTrackingMode));
}
final Object userLocationAnchor = data.get("userLocationAnchor");
if (userLocationAnchor != null) {
final List userLocationAnchorData = toList(userLocationAnchor);
final Point point = toPoint(userLocationAnchorData, metrics.density);
sink.setUserLocationAnchor(point.x, point.y);
}
final Object myLocationRenderMode = data.get("myLocationRenderMode");
if (myLocationRenderMode != null) {
sink.setMyLocationRenderMode(toInt(myLocationRenderMode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ final class MapLibreMapController
private boolean trackCameraPosition = false;
private boolean myLocationEnabled = false;
private int myLocationTrackingMode = 0;
private PointF userLocationAnchorOffset = null;
private int myLocationRenderMode = 0;
private boolean disposed = false;
private boolean dragEnabled = true;
Expand Down Expand Up @@ -690,6 +691,14 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
result.success(Convert.toJson(getCameraPosition()));
break;
}
case "map#setUserLocationAnchor":
{
float x = call.argument("x");
float y = call.argument("y");
setUserLocationAnchor(x, y);
result.success(null);
break;
}
case "map#updateMyLocationTrackingMode":
{
int myLocationTrackingMode = call.argument("mode");
Expand Down Expand Up @@ -1936,6 +1945,13 @@ public void setMyLocationRenderMode(int myLocationRenderMode) {
}
}

@Override
public void setUserLocationAnchor(float x, float y) {
userLocationAnchorOffset = new PointF(x, y);
Log.d(TAG, "setUserLocationAnchor not supported on Android");

}

public void setLogoViewMargins(int x, int y) {
mapLibreMap.getUiSettings().setLogoMargins(x, 0, 0, y);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,

private var initialTilt: CGFloat?
private var cameraTargetBounds: MLNCoordinateBounds?
private var userLocationAnchorOffset: CGPoint?
private var trackCameraPosition = false
private var myLocationEnabled = false
private var scrollingEnabled = true
Expand Down Expand Up @@ -125,6 +126,13 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
return true
}

func mapViewUserLocationAnchorPoint(_ mapView: MLNMapView) -> CGPoint {
if let offset = userLocationAnchorOffset {
return offset
}
return CGPoint(x: mapView.bounds.midX, y: mapView.bounds.midY)
}

func onMethodCall(methodCall: FlutterMethodCall, result: @escaping FlutterResult) {
switch methodCall.method {
case "map#waitForMap":
Expand Down Expand Up @@ -167,6 +175,12 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
result(nil)
}
}
case "map#setUserLocationAnchor":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let x = arguments["x"] as? Double else { return }
guard let y = arguments["y"] as? Double else { return }
setUserLocationAnchor(x: x, y: y)
result(nil)
case "map#updateMyLocationTrackingMode":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
if let myLocationTrackingMode = arguments["mode"] as? UInt,
Expand Down Expand Up @@ -1753,6 +1767,10 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
}
}

func setUserLocationAnchor(x: Double, y: Double) {
userLocationAnchorOffset = CGPoint(x: x, y: y)
}

func setLogoViewMargins(x: Double, y: Double) {
mapView.logoViewMargins = CGPoint(x: x, y: y)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protocol MapLibreMapOptionsSink {
func setMyLocationEnabled(myLocationEnabled: Bool)
func setMyLocationTrackingMode(myLocationTrackingMode: MLNUserTrackingMode)
func setMyLocationRenderMode(myLocationRenderMode: MyLocationRenderMode)
func setUserLocationAnchor(anchorPoint: CGPoint)
func setLogoViewMargins(x: Double, y: Double)
func setCompassViewPosition(position: MLNOrnamentPosition)
func setCompassViewMargins(x: Double, y: Double)
Expand Down
11 changes: 11 additions & 0 deletions maplibre_gl/lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ class MapLibreMapController extends ChangeNotifier {
notifyListeners();
}

/// Set the user location anchor point
///
/// [anchor] is the position of the user location icon relative to
/// the map view.
///
/// The returned [Future] completes after the change has been made on the
/// platform side.
Future<void> setUserLocationAnchor(Point anchor) async {
await _maplibrePlatform.setUserLocationAnchor(anchor);
}

/// Triggers a resize event for the map on web (ignored on Android or iOS).
///
/// Checks first if a resize is required or if it looks like it is already correctly resized.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ abstract class MapLibrePlatform {
Future<CameraPosition?> updateMapOptions(Map<String, dynamic> optionsUpdate);
Future<bool?> animateCamera(CameraUpdate cameraUpdate, {Duration? duration});
Future<bool?> moveCamera(CameraUpdate cameraUpdate);
Future<void> setUserLocationAnchor(Point anchor);
Future<void> updateMyLocationTrackingMode(
MyLocationTrackingMode myLocationTrackingMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ class MapLibreMethodChannel extends MapLibrePlatform {
});
}

@override
Future<void> setUserLocationAnchor(Point anchor) async {
await _channel
.invokeMethod('map#setUserLocationAnchor', <String, dynamic>{
'x': anchor.x,
'y': anchor.y,
});
}

@override
Future<void> updateMyLocationTrackingMode(
MyLocationTrackingMode myLocationTrackingMode) async {
Expand Down
4 changes: 4 additions & 0 deletions maplibre_gl_web/lib/src/convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Convert {
CompassViewPosition.values[options['compassViewPosition']];
sink.setCompassAlignment(position);
}
if (options.containsKey('userLocationAnchor')) {
sink.setUserLocationAnchor(
options['userLocationAnchor'][0], options['userLocationAnchor'][1]);
}
if (options.containsKey('compassViewMargins')) {
sink.setCompassViewMargins(
options['compassViewMargins'][0], options['compassViewMargins'][1]);
Expand Down
6 changes: 6 additions & 0 deletions maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,12 @@ class MapLibreMapController extends MapLibrePlatform
}
}

@override
Future<void> setUserLocationAnchor(Point anchor) async {
print('setUserLocationAnchor not available in web');
return;
}

@override
void setStyleString(String? styleString) {
//remove old mouseenter callbacks to avoid multicalling
Expand Down
2 changes: 2 additions & 0 deletions maplibre_gl_web/lib/src/options_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class MapLibreMapOptionsSink {

void setMyLocationRenderMode(int myLocationRenderMode);

void setUserLocationAnchor(double x, double y);

void setLogoViewMargins(int x, int y);

void setCompassAlignment(CompassViewPosition position);
Expand Down
Loading