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

Commit bb65f55

Browse files
committed
Add drag events
1 parent 72ca4b1 commit bb65f55

File tree

11 files changed

+162
-9
lines changed

11 files changed

+162
-9
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+
## 2.0.4
2+
3+
* Add additional marker drag events.
4+
15
## 2.0.3
26

37
* Fix incorrect typecast in TileOverlay example.

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
@@ -467,10 +467,14 @@ public boolean onMarkerClick(Marker marker) {
467467
}
468468

469469
@Override
470-
public void onMarkerDragStart(Marker marker) {}
470+
public void onMarkerDragStart(Marker marker) {
471+
markersController.onMarkerDragStart(marker.getId(), marker.getPosition());
472+
}
471473

472474
@Override
473-
public void onMarkerDrag(Marker marker) {}
475+
public void onMarkerDrag(Marker marker) {
476+
markersController.onMarkerDrag(marker.getId(), marker.getPosition());
477+
}
474478

475479
@Override
476480
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
@@ -509,6 +509,16 @@ - (void)mapView:(GMSMapView*)mapView didEndDraggingMarker:(GMSMarker*)marker {
509509
[_markersController onMarkerDragEnd:markerId coordinate:marker.position];
510510
}
511511

512+
- (void)mapView:(GMSMapView*)mapView didStartDraggingMarker:(GMSMarker*)marker {
513+
NSString* markerId = marker.userData[0];
514+
[_markersController onMarkerDragStart:markerId coordinate:marker.position];
515+
}
516+
517+
- (void)mapView:(GMSMapView*)mapView didDragMarker:(GMSMarker*)marker {
518+
NSString* markerId = marker.userData[0];
519+
[_markersController onMarkerDrag:markerId coordinate:marker.position];
520+
}
521+
512522
- (void)mapView:(GMSMapView*)mapView didTapInfoWindowOfMarker:(GMSMarker*)marker {
513523
NSString* markerId = marker.userData[0];
514524
[_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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ class GoogleMapController {
6969
GoogleMapsFlutterPlatform.instance
7070
.onMarkerTap(mapId: mapId)
7171
.listen((MarkerTapEvent e) => _googleMapState.onMarkerTap(e.value));
72-
GoogleMapsFlutterPlatform.instance.onMarkerDragEnd(mapId: mapId).listen(
73-
(MarkerDragEndEvent e) =>
74-
_googleMapState.onMarkerDragEnd(e.value, e.position));
72+
GoogleMapsFlutterPlatform.instance
73+
.onMarkerDragStart(mapId: mapId)
74+
.listen((MarkerDragStartEvent e) => _googleMapState.onMarkerDragStart(e.value, e.position));
75+
GoogleMapsFlutterPlatform.instance
76+
.onMarkerDrag(mapId: mapId)
77+
.listen((MarkerDragEvent e) => _googleMapState.onMarkerDrag(e.value, e.position));
78+
GoogleMapsFlutterPlatform.instance
79+
.onMarkerDragEnd(mapId: mapId)
80+
.listen((MarkerDragEndEvent e) => _googleMapState.onMarkerDragEnd(e.value, e.position));
7581
GoogleMapsFlutterPlatform.instance.onInfoWindowTap(mapId: mapId).listen(
7682
(InfoWindowTapEvent e) => _googleMapState.onInfoWindowTap(e.value));
7783
GoogleMapsFlutterPlatform.instance

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

Lines changed: 25 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
@@ -368,6 +368,30 @@ class _GoogleMapState extends State<GoogleMap> {
368368
}
369369
}
370370

371+
void onMarkerDragStart(MarkerId markerId, LatLng position) {
372+
assert(markerId != null);
373+
final Marker? marker = _markers[markerId];
374+
if (marker == null) {
375+
throw UnknownMapObjectIdError('marker', markerId, 'onDragStart');
376+
}
377+
final ValueChanged<LatLng>? onDragStart = marker.onDragStart;
378+
if (onDragStart != null) {
379+
onDragStart(position);
380+
}
381+
}
382+
383+
void onMarkerDrag(MarkerId markerId, LatLng position) {
384+
assert(markerId != null);
385+
final Marker? marker = _markers[markerId];
386+
if (marker == null) {
387+
throw UnknownMapObjectIdError('marker', markerId, 'onDrag');
388+
}
389+
final ValueChanged<LatLng>? onDrag = marker.onDrag;
390+
if (onDrag != null) {
391+
onDrag(position);
392+
}
393+
}
394+
371395
void onMarkerDragEnd(MarkerId markerId, LatLng position) {
372396
assert(markerId != null);
373397
final Marker? marker = _markers[markerId];

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: 2.0.3
4+
version: 2.0.4
55

66
dependencies:
77
flutter:
88
sdk: flutter
99
flutter_plugin_android_lifecycle: ^2.0.1
10-
google_maps_flutter_platform_interface: ^2.0.0
10+
google_maps_flutter_platform_interface: ^2.0.4
1111

1212
dev_dependencies:
1313
flutter_test:

0 commit comments

Comments
 (0)