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

Commit 7d49770

Browse files
committed
Google map marker dragging events
1 parent 91e1c38 commit 7d49770

File tree

10 files changed

+139
-3
lines changed

10 files changed

+139
-3
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
@@ -2,6 +2,10 @@
22

33
* Rename 'Page' in the example app to avoid type conflict with the Flutter Framework.
44

5+
## 0.5.25+4
6+
7+
* Add additional marker drag events
8+
59
## 0.5.25+2
610

711
* Avoid unnecessary map elements updates by ignoring not platform related attributes (eg. onTap)

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
@@ -506,10 +506,14 @@ public boolean onMarkerClick(Marker marker) {
506506
}
507507

508508
@Override
509-
public void onMarkerDragStart(Marker marker) {}
509+
public void onMarkerDragStart(Marker marker) {
510+
markersController.onMarkerDragStart(marker.getId(), marker.getPosition());
511+
}
510512

511513
@Override
512-
public void onMarkerDrag(Marker marker) {}
514+
public void onMarkerDrag(Marker marker) {
515+
markersController.onMarkerDrag(marker.getId(), marker.getPosition());
516+
}
513517

514518
@Override
515519
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,62 @@
1+
package io.flutter.plugins.googlemaps;
2+
3+
import com.google.android.gms.internal.maps.zzt;
4+
import com.google.android.gms.maps.GoogleMap;
5+
import com.google.android.gms.maps.internal.IGoogleMapDelegate;
6+
import com.google.android.gms.maps.model.LatLng;
7+
import com.google.android.gms.maps.model.Marker;
8+
import com.google.android.gms.maps.model.MarkerOptions;
9+
10+
import org.junit.Test;
11+
import org.mockito.Mockito;
12+
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import io.flutter.plugin.common.BinaryMessenger;
20+
import io.flutter.plugin.common.MethodChannel;
21+
import io.flutter.plugin.common.MethodCodec;
22+
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.spy;
26+
import static org.mockito.Mockito.when;
27+
28+
public class MarkersControllerTest {
29+
30+
@Test
31+
public void controller_OnMarkerDragStart() {
32+
final MethodChannel methodChannel = spy(new MethodChannel(mock(BinaryMessenger.class), "no-name", mock(MethodCodec.class)));
33+
final MarkersController controller = new MarkersController(methodChannel);
34+
final GoogleMap googleMap = mock(GoogleMap.class);
35+
controller.setGoogleMap(googleMap);
36+
37+
final zzt z = mock(zzt.class);
38+
final Marker marker = new Marker(z);
39+
40+
final String googleMarkerId = "abc123";
41+
42+
when(marker.getId()).thenReturn(googleMarkerId);
43+
when(googleMap.addMarker(any(MarkerOptions.class))).thenReturn(marker);
44+
45+
final LatLng latLng = new LatLng(1.1, 2.2);
46+
final Map<String, String> markerOptions = new HashMap();
47+
markerOptions.put("markerId", googleMarkerId);
48+
49+
final List<Object> markers = Arrays.<Object>asList(markerOptions);
50+
controller.addMarkers(markers);
51+
controller.onMarkerDragStart(googleMarkerId, latLng);
52+
53+
final List<Double> points = new ArrayList();
54+
points.add(latLng.latitude);
55+
points.add(latLng.longitude);
56+
57+
final Map<String, Object> data = new HashMap<>();
58+
data.put("markerId", googleMarkerId);
59+
data.put("position", points);
60+
Mockito.verify(methodChannel).invokeMethod("marker#onDragStart", data);
61+
}
62+
}

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
@@ -458,6 +458,16 @@ - (void)mapView:(GMSMapView*)mapView didEndDraggingMarker:(GMSMarker*)marker {
458458
[_markersController onMarkerDragEnd:markerId coordinate:marker.position];
459459
}
460460

461+
- (void)mapView:(GMSMapView*)mapView didStartDraggingMarker:(GMSMarker*)marker {
462+
NSString* markerId = marker.userData[0];
463+
[_markersController onMarkerDragStart:markerId coordinate:marker.position];
464+
}
465+
466+
- (void)mapView:(GMSMapView*)mapView didDragMarker:(GMSMarker*)marker {
467+
NSString* markerId = marker.userData[0];
468+
[_markersController onMarkerDrag:markerId coordinate:marker.position];
469+
}
470+
461471
- (void)mapView:(GMSMapView*)mapView didTapInfoWindowOfMarker:(GMSMarker*)marker {
462472
NSString* markerId = marker.userData[0];
463473
[_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/lib/src/controller.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class GoogleMapController {
6464
case 'marker#onTap':
6565
_googleMapState.onMarkerTap(call.arguments['markerId']);
6666
break;
67+
case 'marker#onDrag':
68+
_googleMapState.onMarkerDrag(call.arguments['markerId'],
69+
LatLng._fromJson(call.arguments['position']));
70+
break;
71+
case 'marker#onDragStart':
72+
_googleMapState.onMarkerDragStart(call.arguments['markerId'],
73+
LatLng._fromJson(call.arguments['position']));
74+
break;
6775
case 'marker#onDragEnd':
6876
_googleMapState.onMarkerDragEnd(call.arguments['markerId'],
6977
LatLng._fromJson(call.arguments['position']));

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ class _GoogleMapState extends State<GoogleMap> {
326326
}
327327
}
328328

329+
void onMarkerDragStart(String markerIdParam, LatLng position) {
330+
assert(markerIdParam != null);
331+
final MarkerId markerId = MarkerId(markerIdParam);
332+
if (_markers[markerId]?.onDragStart != null) {
333+
_markers[markerId].onDragStart(position);
334+
}
335+
}
336+
329337
void onMarkerDragEnd(String markerIdParam, LatLng position) {
330338
assert(markerIdParam != null);
331339
final MarkerId markerId = MarkerId(markerIdParam);
@@ -334,6 +342,14 @@ class _GoogleMapState extends State<GoogleMap> {
334342
}
335343
}
336344

345+
void onMarkerDrag(String markerIdParam, LatLng position) {
346+
assert(markerIdParam != null);
347+
final MarkerId markerId = MarkerId(markerIdParam);
348+
if (_markers[markerId]?.onDrag != null) {
349+
_markers[markerId].onDrag(position);
350+
}
351+
}
352+
337353
void onPolygonTap(String polygonIdParam) {
338354
assert(polygonIdParam != null);
339355
final PolygonId polygonId = PolygonId(polygonIdParam);

packages/google_maps_flutter/google_maps_flutter/lib/src/marker.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class Marker {
164164
this.visible = true,
165165
this.zIndex = 0.0,
166166
this.onTap,
167+
this.onDrag,
168+
this.onDragStart,
167169
this.onDragEnd,
168170
}) : assert(alpha == null || (0.0 <= alpha && alpha <= 1.0));
169171

@@ -221,9 +223,15 @@ class Marker {
221223
/// Callbacks to receive tap events for markers placed on this map.
222224
final VoidCallback onTap;
223225

226+
/// Signature reporting the new [LatLng] at the start of a drag event.
227+
final ValueChanged<LatLng> onDragStart;
228+
224229
/// Signature reporting the new [LatLng] at the end of a drag event.
225230
final ValueChanged<LatLng> onDragEnd;
226231

232+
/// Signature reporting the new [LatLng] during the drag event.
233+
final ValueChanged<LatLng> onDrag;
234+
227235
/// Creates a new [Marker] object whose values are the same as this instance,
228236
/// unless overwritten by the specified parameters.
229237
Marker copyWith({

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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.25+3
4+
version: 0.5.25+4
55

66
dependencies:
77
flutter:

0 commit comments

Comments
 (0)