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

[google_maps_flutter] Marker dragging events #2653

Merged
merged 1 commit into from
Sep 24, 2021
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.2

* Add additional marker drag events

## 2.1.1

* Method `buildViewWithTextDirection` has been added to the platform interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ class InfoWindowTapEvent extends MapEvent<MarkerId> {
InfoWindowTapEvent(int mapId, MarkerId markerId) : super(mapId, markerId);
}

/// An event fired when a [Marker] is starting to be dragged to a new [LatLng].
class MarkerDragStartEvent extends _PositionedMapEvent<MarkerId> {
/// Build a MarkerDragStart Event triggered from the map represented by `mapId`.
///
/// The `position` on this event is the [LatLng] on which the Marker was picked up from.
/// The `value` of this event is a [MarkerId] object that represents the Marker.
MarkerDragStartEvent(int mapId, LatLng position, MarkerId markerId)
: super(mapId, position, markerId);
}

/// An event fired when a [Marker] is being dragged to a new [LatLng].
class MarkerDragEvent extends _PositionedMapEvent<MarkerId> {
/// Build a MarkerDrag Event triggered from the map represented by `mapId`.
///
/// The `position` on this event is the [LatLng] on which the Marker was dragged to.
/// The `value` of this event is a [MarkerId] object that represents the Marker.
MarkerDragEvent(int mapId, LatLng position, MarkerId markerId)
: super(mapId, position, markerId);
}

/// An event fired when a [Marker] is dragged to a new [LatLng].
class MarkerDragEndEvent extends _PositionedMapEvent<MarkerId> {
/// Build a MarkerDragEnd Event triggered from the map represented by `mapId`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform {
return _events(mapId).whereType<InfoWindowTapEvent>();
}

@override
Stream<MarkerDragStartEvent> onMarkerDragStart({required int mapId}) {
return _events(mapId).whereType<MarkerDragStartEvent>();
}

@override
Stream<MarkerDragEvent> onMarkerDrag({required int mapId}) {
return _events(mapId).whereType<MarkerDragEvent>();
}

@override
Stream<MarkerDragEndEvent> onMarkerDragEnd({required int mapId}) {
return _events(mapId).whereType<MarkerDragEndEvent>();
Expand Down Expand Up @@ -174,6 +184,20 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform {
MarkerId(call.arguments['markerId']),
));
break;
case 'marker#onDragStart':
_mapEventStreamController.add(MarkerDragStartEvent(
mapId,
LatLng.fromJson(call.arguments['position'])!,
MarkerId(call.arguments['markerId']),
));
break;
case 'marker#onDrag':
_mapEventStreamController.add(MarkerDragEvent(
mapId,
LatLng.fromJson(call.arguments['position'])!,
MarkerId(call.arguments['markerId']),
));
break;
case 'marker#onDragEnd':
_mapEventStreamController.add(MarkerDragEndEvent(
mapId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ abstract class GoogleMapsFlutterPlatform extends PlatformInterface {
throw UnimplementedError('onInfoWindowTap() has not been implemented.');
}

/// A [Marker] has been dragged to a different [LatLng] position.
Stream<MarkerDragStartEvent> onMarkerDragStart({required int mapId}) {
throw UnimplementedError('onMarkerDragEnd() has not been implemented.');
}

/// A [Marker] has been dragged to a different [LatLng] position.
Stream<MarkerDragEvent> onMarkerDrag({required int mapId}) {
throw UnimplementedError('onMarkerDragEnd() has not been implemented.');
}

/// A [Marker] has been dragged to a different [LatLng] position.
Stream<MarkerDragEndEvent> onMarkerDragEnd({required int mapId}) {
throw UnimplementedError('onMarkerDragEnd() has not been implemented.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class Marker implements MapsObject {
this.visible = true,
this.zIndex = 0.0,
this.onTap,
this.onDrag,
this.onDragStart,
this.onDragEnd,
}) : assert(alpha == null || (0.0 <= alpha && alpha <= 1.0));

Expand Down Expand Up @@ -207,9 +209,15 @@ class Marker implements MapsObject {
/// Callbacks to receive tap events for markers placed on this map.
final VoidCallback? onTap;

/// Signature reporting the new [LatLng] at the start of a drag event.
final ValueChanged<LatLng>? onDragStart;

/// Signature reporting the new [LatLng] at the end of a drag event.
final ValueChanged<LatLng>? onDragEnd;

/// Signature reporting the new [LatLng] during the drag event.
final ValueChanged<LatLng>? onDrag;

/// Creates a new [Marker] object whose values are the same as this instance,
/// unless overwritten by the specified parameters.
Marker copyWith({
Expand All @@ -225,6 +233,8 @@ class Marker implements MapsObject {
bool? visibleParam,
double? zIndexParam,
VoidCallback? onTapParam,
ValueChanged<LatLng>? onDragStartParam,
ValueChanged<LatLng>? onDragParam,
ValueChanged<LatLng>? onDragEndParam,
}) {
return Marker(
Expand All @@ -241,6 +251,8 @@ class Marker implements MapsObject {
visible: visibleParam ?? visible,
zIndex: zIndexParam ?? zIndex,
onTap: onTapParam ?? onTap,
onDragStart: onDragStartParam ?? onDragStart,
onDrag: onDragParam ?? onDrag,
onDragEnd: onDragEndParam ?? onDragEnd,
);
}
Expand Down Expand Up @@ -300,6 +312,7 @@ class Marker implements MapsObject {
return 'Marker{markerId: $markerId, alpha: $alpha, anchor: $anchor, '
'consumeTapEvents: $consumeTapEvents, draggable: $draggable, flat: $flat, '
'icon: $icon, infoWindow: $infoWindow, position: $position, rotation: $rotation, '
'visible: $visible, zIndex: $zIndex, onTap: $onTap}';
'visible: $visible, zIndex: $zIndex, onTap: $onTap, onDragStart: $onDragStart, '
'onDrag: $onDrag, onDragEnd: $onDragEnd}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/google_maps_
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.1.1
version: 2.1.2

environment:
sdk: '>=2.12.0 <3.0.0'
Expand All @@ -19,6 +19,7 @@ dependencies:
stream_transform: ^2.0.0

dev_dependencies:
async: ^2.5.0
flutter_test:
sdk: flutter
mockito: ^5.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:google_maps_flutter_platform_interface/src/events/map_event.dart';
import 'package:google_maps_flutter_platform_interface/src/method_channel/method_channel_google_maps_flutter.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'dart:async';

import 'package:async/async.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Expand All @@ -33,6 +37,15 @@ void main() {
});
}

Future<void> sendPlatformMessage(
int mapId, String method, Map<dynamic, dynamic> data) async {
final ByteData byteData = const StandardMethodCodec()
.encodeMethodCall(MethodCall(method, data));
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage(
"plugins.flutter.io/google_maps_$mapId", byteData, (data) {});
}

// Calls each method that uses invokeMethod with a return type other than
// void to ensure that the casting/nullability handling succeeds.
//
Expand Down Expand Up @@ -68,5 +81,46 @@ void main() {
'map#takeSnapshot',
]);
});
test('markers send drag event to correct streams', () async {
const int mapId = 1;
final jsonMarkerDragStartEvent = <dynamic, dynamic>{
"mapId": mapId,
"markerId": "drag-start-marker",
"position": <double>[1.0, 1.0]
};
final jsonMarkerDragEvent = <dynamic, dynamic>{
"mapId": mapId,
"markerId": "drag-marker",
"position": <double>[1.0, 1.0]
};
final jsonMarkerDragEndEvent = <dynamic, dynamic>{
"mapId": mapId,
"markerId": "drag-end-marker",
"position": <double>[1.0, 1.0]
};

final MethodChannelGoogleMapsFlutter maps =
MethodChannelGoogleMapsFlutter();
maps.ensureChannelInitialized(mapId);

final StreamQueue<MarkerDragStartEvent> markerDragStartStream =
StreamQueue(maps.onMarkerDragStart(mapId: mapId));
final StreamQueue<MarkerDragEvent> markerDragStream =
StreamQueue(maps.onMarkerDrag(mapId: mapId));
final StreamQueue<MarkerDragEndEvent> markerDragEndStream =
StreamQueue(maps.onMarkerDragEnd(mapId: mapId));

await sendPlatformMessage(
mapId, "marker#onDragStart", jsonMarkerDragStartEvent);
await sendPlatformMessage(mapId, "marker#onDrag", jsonMarkerDragEvent);
await sendPlatformMessage(
mapId, "marker#onDragEnd", jsonMarkerDragEndEvent);

expect((await markerDragStartStream.next).value.value,
equals("drag-start-marker"));
expect((await markerDragStream.next).value.value, equals("drag-marker"));
expect((await markerDragEndStream.next).value.value,
equals("drag-end-marker"));
});
});
}
Loading