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

Commit 56fdb95

Browse files
committed
Add drag events
1 parent a71d033 commit 56fdb95

File tree

11 files changed

+158
-6
lines changed

11 files changed

+158
-6
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.33
2+
3+
* Add additional marker drag events
4+
15
## 0.5.32
26

37
* Fix typo in google_maps_flutter/example/map_ui.dart.

packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,14 @@ public boolean onMarkerClick(Marker marker) {
501501
}
502502

503503
@Override
504-
public void onMarkerDragStart(Marker marker) {}
504+
public void onMarkerDragStart(Marker marker) {
505+
markersController.onMarkerDragStart(marker.getId(), marker.getPosition());
506+
}
505507

506508
@Override
507-
public void onMarkerDrag(Marker marker) {}
509+
public void onMarkerDrag(Marker marker) {
510+
markersController.onMarkerDrag(marker.getId(), marker.getPosition());
511+
}
508512

509513
@Override
510514
public void onMarkerDragEnd(Marker marker) {

packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/MarkersController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ boolean onMarkerTap(String googleMarkerId) {
105105
return false;
106106
}
107107

108+
void onMarkerDragStart(String googleMarkerId, LatLng latLng) {
109+
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
110+
if (markerId == null) {
111+
return;
112+
}
113+
final Map<String, Object> data = new HashMap<>();
114+
data.put("markerId", markerId);
115+
data.put("position", Convert.latLngToJson(latLng));
116+
methodChannel.invokeMethod("marker#onDragStart", data);
117+
}
118+
119+
void onMarkerDrag(String googleMarkerId, LatLng latLng) {
120+
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
121+
if (markerId == null) {
122+
return;
123+
}
124+
final Map<String, Object> data = new HashMap<>();
125+
data.put("markerId", markerId);
126+
data.put("position", Convert.latLngToJson(latLng));
127+
methodChannel.invokeMethod("marker#onDrag", data);
128+
}
129+
108130
void onMarkerDragEnd(String googleMarkerId, LatLng latLng) {
109131
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
110132
if (markerId == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.flutter.plugins.googlemaps;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.when;
7+
8+
import com.google.android.gms.internal.maps.zzt;
9+
import com.google.android.gms.maps.GoogleMap;
10+
import com.google.android.gms.maps.model.LatLng;
11+
import com.google.android.gms.maps.model.Marker;
12+
import com.google.android.gms.maps.model.MarkerOptions;
13+
import io.flutter.plugin.common.BinaryMessenger;
14+
import io.flutter.plugin.common.MethodChannel;
15+
import io.flutter.plugin.common.MethodCodec;
16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
import org.junit.Test;
22+
import org.mockito.Mockito;
23+
24+
public class MarkersControllerTest {
25+
26+
@Test
27+
public void controller_OnMarkerDragStart() {
28+
final MethodChannel methodChannel =
29+
spy(new MethodChannel(mock(BinaryMessenger.class), "no-name", mock(MethodCodec.class)));
30+
final MarkersController controller = new MarkersController(methodChannel);
31+
final GoogleMap googleMap = mock(GoogleMap.class);
32+
controller.setGoogleMap(googleMap);
33+
34+
final zzt z = mock(zzt.class);
35+
final Marker marker = new Marker(z);
36+
37+
final String googleMarkerId = "abc123";
38+
39+
when(marker.getId()).thenReturn(googleMarkerId);
40+
when(googleMap.addMarker(any(MarkerOptions.class))).thenReturn(marker);
41+
42+
final LatLng latLng = new LatLng(1.1, 2.2);
43+
final Map<String, String> markerOptions = new HashMap();
44+
markerOptions.put("markerId", googleMarkerId);
45+
46+
final List<Object> markers = Arrays.<Object>asList(markerOptions);
47+
controller.addMarkers(markers);
48+
controller.onMarkerDragStart(googleMarkerId, latLng);
49+
50+
final List<Double> points = new ArrayList();
51+
points.add(latLng.latitude);
52+
points.add(latLng.longitude);
53+
54+
final Map<String, Object> data = new HashMap<>();
55+
data.put("markerId", googleMarkerId);
56+
data.put("position", points);
57+
Mockito.verify(methodChannel).invokeMethod("marker#onDragStart", data);
58+
}
59+
}

packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PlaceMarkerBody extends StatefulWidget {
2929
State<StatefulWidget> createState() => PlaceMarkerBodyState();
3030
}
3131

32-
typedef Marker MarkerUpdateAction(Marker marker);
32+
typedef MarkerUpdateAction = Marker Function(Marker marker);
3333

3434
class PlaceMarkerBodyState extends State<PlaceMarkerBody> {
3535
PlaceMarkerBodyState();

packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapController.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ - (void)mapView:(GMSMapView*)mapView didEndDraggingMarker:(GMSMarker*)marker {
479479
[_markersController onMarkerDragEnd:markerId coordinate:marker.position];
480480
}
481481

482+
- (void)mapView:(GMSMapView*)mapView didStartDraggingMarker:(GMSMarker*)marker {
483+
NSString* markerId = marker.userData[0];
484+
[_markersController onMarkerDragStart:markerId coordinate:marker.position];
485+
}
486+
487+
- (void)mapView:(GMSMapView*)mapView didDragMarker:(GMSMarker*)marker {
488+
NSString* markerId = marker.userData[0];
489+
[_markersController onMarkerDrag:markerId coordinate:marker.position];
490+
}
491+
482492
- (void)mapView:(GMSMapView*)mapView didTapInfoWindowOfMarker:(GMSMarker*)marker {
483493
NSString* markerId = marker.userData[0];
484494
[_markersController onInfoWindowTap:markerId];

packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapMarkerController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ NS_ASSUME_NONNULL_BEGIN
4545
- (void)changeMarkers:(NSArray*)markersToChange;
4646
- (void)removeMarkerIds:(NSArray*)markerIdsToRemove;
4747
- (BOOL)onMarkerTap:(NSString*)markerId;
48+
- (void)onMarkerDragStart:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate;
4849
- (void)onMarkerDragEnd:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate;
50+
- (void)onMarkerDrag:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate;
4951
- (void)onInfoWindowTap:(NSString*)markerId;
5052
- (void)showMarkerInfoWindow:(NSString*)markerId result:(FlutterResult)result;
5153
- (void)hideMarkerInfoWindow:(NSString*)markerId result:(FlutterResult)result;

packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapMarkerController.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ - (BOOL)onMarkerTap:(NSString*)markerId {
295295
[_methodChannel invokeMethod:@"marker#onTap" arguments:@{@"markerId" : markerId}];
296296
return controller.consumeTapEvents;
297297
}
298+
- (void)onMarkerDragStart:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate {
299+
if (!markerId) {
300+
return;
301+
}
302+
FLTGoogleMapMarkerController* controller = _markerIdToController[markerId];
303+
if (!controller) {
304+
return;
305+
}
306+
[_methodChannel invokeMethod:@"marker#onDragStart"
307+
arguments:@{@"markerId" : markerId, @"position" : PositionToJson(coordinate)}];
308+
}
309+
- (void)onMarkerDrag:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate {
310+
if (!markerId) {
311+
return;
312+
}
313+
FLTGoogleMapMarkerController* controller = _markerIdToController[markerId];
314+
if (!controller) {
315+
return;
316+
}
317+
[_methodChannel invokeMethod:@"marker#onDrag"
318+
arguments:@{@"markerId" : markerId, @"position" : PositionToJson(coordinate)}];
319+
}
298320
- (void)onMarkerDragEnd:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate {
299321
if (!markerId) {
300322
return;

packages/google_maps_flutter/google_maps_flutter/lib/src/controller.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,38 @@ class GoogleMapController {
7171
_googleMapsFlutterPlatform
7272
.onMarkerTap(mapId: mapId)
7373
.listen((MarkerTapEvent e) => _googleMapState.onMarkerTap(e.value));
74+
75+
_googleMapsFlutterPlatform.onMarkerDragStart(mapId: mapId).listen(
76+
(MarkerDragStartEvent e) =>
77+
_googleMapState.onMarkerDragStart(e.value, e.position));
78+
79+
_googleMapsFlutterPlatform.onMarkerDrag(mapId: mapId).listen(
80+
(MarkerDragEvent e) =>
81+
_googleMapState.onMarkerDrag(e.value, e.position));
82+
7483
_googleMapsFlutterPlatform.onMarkerDragEnd(mapId: mapId).listen(
7584
(MarkerDragEndEvent e) =>
7685
_googleMapState.onMarkerDragEnd(e.value, e.position));
86+
7787
_googleMapsFlutterPlatform.onInfoWindowTap(mapId: mapId).listen(
7888
(InfoWindowTapEvent e) => _googleMapState.onInfoWindowTap(e.value));
89+
7990
_googleMapsFlutterPlatform
8091
.onPolylineTap(mapId: mapId)
8192
.listen((PolylineTapEvent e) => _googleMapState.onPolylineTap(e.value));
93+
8294
_googleMapsFlutterPlatform
8395
.onPolygonTap(mapId: mapId)
8496
.listen((PolygonTapEvent e) => _googleMapState.onPolygonTap(e.value));
97+
8598
_googleMapsFlutterPlatform
8699
.onCircleTap(mapId: mapId)
87100
.listen((CircleTapEvent e) => _googleMapState.onCircleTap(e.value));
101+
88102
_googleMapsFlutterPlatform
89103
.onTap(mapId: mapId)
90104
.listen((MapTapEvent e) => _googleMapState.onTap(e.position));
105+
91106
_googleMapsFlutterPlatform.onLongPress(mapId: mapId).listen(
92107
(MapLongPressEvent e) => _googleMapState.onLongPress(e.position));
93108
}

packages/google_maps_flutter/google_maps_flutter/lib/src/google_map.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ part of google_maps_flutter;
88
///
99
/// Pass to [GoogleMap.onMapCreated] to receive a [GoogleMapController] when the
1010
/// map is created.
11-
typedef void MapCreatedCallback(GoogleMapController controller);
11+
typedef MapCreatedCallback = void Function(GoogleMapController controller);
1212

1313
// This counter is used to provide a stable "constant" initialization id
1414
// to the buildView function, so the web implementation can use it as a
@@ -332,6 +332,20 @@ class _GoogleMapState extends State<GoogleMap> {
332332
}
333333
}
334334

335+
void onMarkerDragStart(MarkerId markerId, LatLng position) {
336+
assert(markerId != null);
337+
if (_markers[markerId]?.onDragStart != null) {
338+
_markers[markerId].onDragStart(position);
339+
}
340+
}
341+
342+
void onMarkerDrag(MarkerId markerId, LatLng position) {
343+
assert(markerId != null);
344+
if (_markers[markerId]?.onDrag != null) {
345+
_markers[markerId].onDrag(position);
346+
}
347+
}
348+
335349
void onMarkerDragEnd(MarkerId markerId, LatLng position) {
336350
assert(markerId != null);
337351
if (_markers[markerId]?.onDragEnd != null) {

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
4-
version: 0.5.32
4+
version: 0.5.33
55

66
dependencies:
77
flutter:
88
sdk: flutter
99
flutter_plugin_android_lifecycle: ^1.0.0
10-
google_maps_flutter_platform_interface: ^1.0.4
10+
google_maps_flutter_platform_interface: ^1.0.5
1111

1212
dev_dependencies:
1313
flutter_test:

0 commit comments

Comments
 (0)