Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
4 changes: 4 additions & 0 deletions packages/google_maps_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.20+4

* Marker drag event

## 0.5.20+3

* Update Android play-services-maps to 17.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class GoogleMapController
OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerDragListener,
PlatformView {

private static final String TAG = "GoogleMapController";
Expand Down Expand Up @@ -177,6 +178,7 @@ public void onMapReady(GoogleMap googleMap) {
googleMap.setOnCameraMoveListener(this);
googleMap.setOnCameraIdleListener(this);
googleMap.setOnMarkerClickListener(this);
googleMap.setOnMarkerDragListener(this);
googleMap.setOnPolygonClickListener(this);
googleMap.setOnPolylineClickListener(this);
googleMap.setOnCircleClickListener(this);
Expand Down Expand Up @@ -395,6 +397,17 @@ public boolean onMarkerClick(Marker marker) {
return markersController.onMarkerTap(marker.getId());
}

@Override
public void onMarkerDragStart(Marker marker) {}

@Override
public void onMarkerDrag(Marker marker) {}

@Override
public void onMarkerDragEnd(Marker marker) {
markersController.onMarkerDragEnd(marker.getId(), marker.getPosition());
}

@Override
public void onPolygonClick(Polygon polygon) {
polygonsController.onPolygonTap(polygon.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -75,6 +76,17 @@ boolean onMarkerTap(String googleMarkerId) {
return false;
}

void onMarkerDragEnd(String googleMarkerId, LatLng latLng) {
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
if (markerId == null) {
return;
}
final Map<String, Object> data = new HashMap<>();
data.put("markerId", markerId);
data.put("position", Convert.latLngToJson(latLng));
methodChannel.invokeMethod("marker#onDragEnd", data);
}

void onInfoWindowTap(String googleMarkerId) {
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
if (markerId == null) {
Expand Down
29 changes: 29 additions & 0 deletions packages/google_maps_flutter/example/lib/place_marker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> {
}
}

void _onMarkerDragEnd(MarkerId markerId, LatLng newPosition) async {
final Marker tappedMarker = markers[markerId];
if (tappedMarker != null) {
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
actions: <Widget>[
FlatButton(
child: const Text('OK'),
onPressed: () => Navigator.of(context).pop(),
)
],
content: Padding(
padding: const EdgeInsets.symmetric(vertical: 66),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Old position: ${tappedMarker.position}'),
Text('New position: $newPosition'),
],
)));
});
}
}

void _add() {
final int markerCount = markers.length;

Expand All @@ -88,6 +114,9 @@ class PlaceMarkerBodyState extends State<PlaceMarkerBody> {
onTap: () {
_onMarkerTapped(markerId);
},
onDragEnd: (LatLng position) {
_onMarkerDragEnd(markerId, position);
},
);

setState(() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ - (BOOL)mapView:(GMSMapView*)mapView didTapMarker:(GMSMarker*)marker {
return [_markersController onMarkerTap:markerId];
}

- (void)mapView:(GMSMapView*)mapView didEndDraggingMarker:(GMSMarker*)marker {
NSString* markerId = marker.userData[0];
[_markersController onMarkerDragEnd:markerId coordinate:marker.position];
}

- (void)mapView:(GMSMapView*)mapView didTapInfoWindowOfMarker:(GMSMarker*)marker {
NSString* markerId = marker.userData[0];
[_markersController onInfoWindowTap:markerId];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
- (void)changeMarkers:(NSArray*)markersToChange;
- (void)removeMarkerIds:(NSArray*)markerIdsToRemove;
- (BOOL)onMarkerTap:(NSString*)markerId;
- (void)onMarkerDragEnd:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate;
- (void)onInfoWindowTap:(NSString*)markerId;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ - (BOOL)onMarkerTap:(NSString*)markerId {
[_methodChannel invokeMethod:@"marker#onTap" arguments:@{@"markerId" : markerId}];
return controller.consumeTapEvents;
}
- (void)onMarkerDragEnd:(NSString*)markerId coordinate:(CLLocationCoordinate2D)coordinate {
if (!markerId) {
return;
}
FLTGoogleMapMarkerController* controller = _markerIdToController[markerId];
if (!controller) {
return;
}
[_methodChannel invokeMethod:@"marker#onDragEnd"
arguments:@{@"markerId" : markerId, @"position" : PositionToJson(coordinate)}];
}
- (void)onInfoWindowTap:(NSString*)markerId {
if (markerId && _markerIdToController[markerId]) {
[_methodChannel invokeMethod:@"infoWindow#onTap" arguments:@{@"markerId" : markerId}];
Expand Down
4 changes: 4 additions & 0 deletions packages/google_maps_flutter/lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class GoogleMapController {
case 'marker#onTap':
_googleMapState.onMarkerTap(call.arguments['markerId']);
break;
case 'marker#onDragEnd':
_googleMapState.onMarkerDragEnd(call.arguments['markerId'],
LatLng._fromJson(call.arguments['position']));
break;
case 'infoWindow#onTap':
_googleMapState.onInfoWindowTap(call.arguments['markerId']);
break;
Expand Down
8 changes: 8 additions & 0 deletions packages/google_maps_flutter/lib/src/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ class _GoogleMapState extends State<GoogleMap> {
}
}

void onMarkerDragEnd(String markerIdParam, LatLng position) {
assert(markerIdParam != null);
final MarkerId markerId = MarkerId(markerIdParam);
if (_markers[markerId]?.onDragEnd != null) {
_markers[markerId].onDragEnd(position);
}
}

void onPolygonTap(String polygonIdParam) {
assert(polygonIdParam != null);
final PolygonId polygonId = PolygonId(polygonIdParam);
Expand Down
5 changes: 5 additions & 0 deletions packages/google_maps_flutter/lib/src/marker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class Marker {
this.visible = true,
this.zIndex = 0.0,
this.onTap,
this.onDragEnd,
}) : assert(alpha == null || (0.0 <= alpha && alpha <= 1.0));

/// Uniquely identifies a [Marker].
Expand Down Expand Up @@ -216,6 +217,8 @@ class Marker {
/// Callbacks to receive tap events for markers placed on this map.
final VoidCallback onTap;

final ValueChanged<LatLng> onDragEnd;

/// Creates a new [Marker] object whose values are the same as this instance,
/// unless overwritten by the specified parameters.
Marker copyWith({
Expand All @@ -231,6 +234,7 @@ class Marker {
bool visibleParam,
double zIndexParam,
VoidCallback onTapParam,
ValueChanged<LatLng> onDragEndParam,
}) {
return Marker(
markerId: markerId,
Expand All @@ -246,6 +250,7 @@ class Marker {
visible: visibleParam ?? visible,
zIndex: zIndexParam ?? zIndex,
onTap: onTapParam ?? onTap,
onDragEnd: onDragEndParam ?? onDragEnd,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/google_maps_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_maps_flutter
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter
version: 0.5.20+3
version: 0.5.20+4

dependencies:
flutter:
Expand Down