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

Expose CameraChanged/cameraState #704

Merged
merged 4 commits into from
Sep 30, 2024
Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
### main

* Expose current map's camera state on `CameraChanged` event.

You can now observe the map's camera updates with `onCameraChangeListener`

```dart
onCameraChangeListener(CameraChangedEventData data) {
print("CameraChangedEventData: timestamp: ${data.timestamp}, cameraState: ${data.cameraState}");
}
```

### 2.3.0-rc.1

* Expose API to clear map data, and to set options to `TileStore`.
Expand Down
16 changes: 15 additions & 1 deletion example/lib/full_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FullMapState extends State<FullMap> {
}

_onCameraChangeListener(CameraChangedEventData data) {
print("CameraChangedEventData: timestamp: ${data.timestamp}");
print("CameraChangedEventData: ${data.debugInfo}");
}

_onResourceRequestListener(ResourceEventData data) {
Expand Down Expand Up @@ -142,3 +142,17 @@ class FullMapState extends State<FullMap> {
));
}
}

extension on CameraChangedEventData {

String get debugInfo {
return "timestamp ${DateTime.fromMicrosecondsSinceEpoch(timestamp)}, camera: ${cameraState.debugInfo}";
}
}

extension on CameraState {

String get debugInfo {
return "lat: ${center.coordinates.lat}, lng: ${center.coordinates.lng}, zoom: ${zoom}, bearing: ${bearing}, pitch: ${pitch}";
}
}
16 changes: 15 additions & 1 deletion ios/Classes/MapEvents+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ extension EventTimeInterval {
}
}

extension MapboxMaps.CameraState {

var toJSON: [String: Any] {
return [
"center": center.toDict(),
"padding": ["top": padding.top, "left": padding.left, "bottom": padding.bottom, "right": padding.right],
"zoom": zoom,
"bearing": bearing,
"pitch": pitch
]
}
}

extension MapboxMaps.CanonicalTileID {
var toJSON: [String: Any] {
[
Expand Down Expand Up @@ -65,7 +78,8 @@ extension StyleDataLoaded: MapEventEncodable {
extension CameraChanged: MapEventEncodable {
var toJSON: [String: Any?] {
[
"timestamp": timestamp.microsecondsSince1970
"timestamp": timestamp.microsecondsSince1970,
"cameraState": cameraState.toJSON,
]
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ class EventTimeInterval {
class CameraChangedEventData {
/// The time when the camera was changed.
final int timestamp;
/// The current state of the camera.
final CameraState cameraState;

CameraChangedEventData.fromJson(Map<String, dynamic> json)
: timestamp = json['timestamp'];
: timestamp = json['timestamp'],
cameraState = Conversion.fromJson(json['cameraState']);
}

/// The class for map-idle event in Observer
Expand Down
21 changes: 21 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ extension Conversion on CameraState {
pitch: pitch,
);
}

static CameraState fromJson(Map<String, dynamic> json) {
return CameraState(
center: Point.fromJson(json['center']),
padding: MbxEdgeInsetsCodable.fromJson(json['padding']),
zoom: json['zoom'].toDouble(),
bearing: json['bearing'].toDouble(),
pitch: json['pitch'].toDouble());
}
}

extension MbxEdgeInsetsCodable on MbxEdgeInsets {

static MbxEdgeInsets fromJson(Map<String, dynamic> json) {
return MbxEdgeInsets(
top: json["top"].toDouble(),
left: json["left"].toDouble(),
bottom: json["bottom"].toDouble(),
right: json["right"].toDouble(),
);
}
}

extension ScreenBoxToJson on ScreenBox {
Expand Down
21 changes: 19 additions & 2 deletions test/events_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@ import 'package:test/test.dart';

void main() {
test('CameraChangedEventData fromJson', () {
var cameraChangedEventData =
CameraChangedEventData.fromJson(<String, dynamic>{'timestamp': 1});
final json = {
'timestamp': 1,
'cameraState': {
'center': {'coordinates': [0, 0]},
'padding': {'top': 0, 'left': 1, 'bottom': 2, 'right': 3},
'zoom': 11,
'bearing': 0,
'pitch': 0,
},
};
var cameraChangedEventData = CameraChangedEventData.fromJson(json);
expect(cameraChangedEventData.timestamp, 1);
expect(cameraChangedEventData.cameraState.center.coordinates, Position.of([0, 0]));
expect(cameraChangedEventData.cameraState.padding.top, 0);
expect(cameraChangedEventData.cameraState.padding.left, 1);
expect(cameraChangedEventData.cameraState.padding.bottom, 2);
expect(cameraChangedEventData.cameraState.padding.right, 3);
expect(cameraChangedEventData.cameraState.zoom, 11);
expect(cameraChangedEventData.cameraState.bearing, 0);
expect(cameraChangedEventData.cameraState.pitch, 0);
});

test('MapIdleEventData fromJson', () {
Expand Down