Skip to content

Commit

Permalink
clean up web interop
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Sep 25, 2024
1 parent 7b9bf29 commit 7504b8b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
14 changes: 14 additions & 0 deletions lib/src/web/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:maplibre/maplibre.dart';
import 'package:maplibre/src/web/interop/interop.dart' as interop;

Expand All @@ -15,3 +17,15 @@ extension LngLatBoundsExt on LngLatBounds {
interop.LngLat(lng: longitudeEast, lat: latitudeNorth),
);
}

/// Extension methods for the [Offset] class. Not exported publicly.
extension OffsetExt on Offset {
/// Convert a [Offset] to an internal [interop.Point].
interop.Point toJsPoint() => interop.Point(dx, dy);
}

/// Extension methods for the [interop.Point] class. Not exported publicly.
extension JsPointExt on interop.Point {
/// Convert a [interop.Point] to an internal [Offset].
Offset toOffset() => Offset(x.toDouble(), y.toDouble());
}
6 changes: 6 additions & 0 deletions lib/src/web/interop/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ extension type Camera._(JSObject _) implements JSObject {
FlyToOptions options, [
JSAny? eventData,
]);

/// Animate the viewport of the map to fit [LngLatBounds].
external void fitBounds(
LngLatBounds bounds,
FitBoundsOptions? options,
);
}

/// Options used for [Camera.jumpTo].
Expand Down
6 changes: 3 additions & 3 deletions lib/src/web/interop/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ extension type MapOptions._(JSObject _) implements JSObject {
/// Options to specify the map bounds.
@anonymous
@JS()
extension type FitBoundsOptions._(JSObject _) implements JSObject {
extension type FitBoundsOptions._(FlyToOptions _) implements FlyToOptions {
/// Create a new JS [FitBoundsOptions] object.
external FitBoundsOptions({
bool? linear,
// TODO Offset? offset,
Point? offset,
num? maxZoom,
// TODO All parameters from FlyToOptions
num? maxDuration,
});
}

Expand Down
14 changes: 4 additions & 10 deletions lib/src/web/widget_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,12 @@ final class MapLibreMapStateWeb extends State<MapLibreMap>
}

@override
Future<Position> toLngLat(Offset screenLocation) async {
final lngLat = _map.unproject(
interop.Point(screenLocation.dx, screenLocation.dy),
);
return lngLat.toPosition();
}
Future<Position> toLngLat(Offset screenLocation) async =>
_map.unproject(screenLocation.toJsPoint()).toPosition();

@override
Future<Offset> toScreenLocation(Position lngLat) async {
final screenPosition = _map.project(lngLat.toLngLat());
return Offset(screenPosition.x.toDouble(), screenPosition.y.toDouble());
}
Future<Offset> toScreenLocation(Position lngLat) async =>
_map.project(lngLat.toLngLat()).toOffset();

@override
Future<void> jumpTo({
Expand Down

0 comments on commit 7504b8b

Please sign in to comment.