Skip to content

Commit c165694

Browse files
authored
fix: marker drag event handling (#567)
* Fix marker drag event handling by implementing missing DTO conversion. * Add missing flutter unit tests for events * Fix POI event variable naming for placeID
1 parent 3c48de5 commit c165694

32 files changed

+3123
-69
lines changed

android/src/main/kotlin/com/google/maps/flutter/navigation/GoogleMapsBaseMapView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ abstract class GoogleMapsBaseMapView(
247247
viewEventApi?.onPoiClick(
248248
getViewId().toLong(),
249249
PointOfInterestDto(
250-
placeId = pointOfInterest.placeId,
250+
placeID = pointOfInterest.placeId,
251251
name = pointOfInterest.name,
252252
latLng = LatLngDto(pointOfInterest.latLng.latitude, pointOfInterest.latLng.longitude),
253253
),

android/src/main/kotlin/com/google/maps/flutter/navigation/messages.g.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,23 +1036,23 @@ data class PointOfInterestDto(
10361036
* The Place ID of this POI, as defined in the Places SDK. This can be used to retrieve additional
10371037
* information about the place.
10381038
*/
1039-
val placeId: String,
1039+
val placeID: String,
10401040
/** The name of the POI (e.g., "Central Park", "City Hall"). */
10411041
val name: String,
10421042
/** The geographical coordinates of the POI. */
10431043
val latLng: LatLngDto,
10441044
) {
10451045
companion object {
10461046
fun fromList(pigeonVar_list: List<Any?>): PointOfInterestDto {
1047-
val placeId = pigeonVar_list[0] as String
1047+
val placeID = pigeonVar_list[0] as String
10481048
val name = pigeonVar_list[1] as String
10491049
val latLng = pigeonVar_list[2] as LatLngDto
1050-
return PointOfInterestDto(placeId, name, latLng)
1050+
return PointOfInterestDto(placeID, name, latLng)
10511051
}
10521052
}
10531053

10541054
fun toList(): List<Any?> {
1055-
return listOf(placeId, name, latLng)
1055+
return listOf(placeID, name, latLng)
10561056
}
10571057

10581058
override fun equals(other: Any?): Boolean {

example/integration_test/t06_map_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void main() {
189189

190190
void onPoiClicked(PointOfInterest poi) {
191191
$.log(
192-
'POI clicked event: placeId=${poi.placeId}, name=${poi.name}, '
192+
'POI clicked event: placeId=${poi.placeID}, name=${poi.name}, '
193193
'lat=${poi.latLng.latitude}, lng=${poi.latLng.longitude}.',
194194
);
195195
}

example/lib/pages/markers.dart

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
3939

4040
late bool _isMapToolbarEnabled = true;
4141
late bool _displayMarkerUpdates = false;
42+
String _latestMarkerDragUpdate = '';
4243
final List<double> _zIndexes = <double>[-1, 0, 1];
4344
final List<double> _alphas = <double>[1.0, 0.3];
4445

@@ -215,15 +216,30 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
215216
}
216217

217218
void _onMarkerDrag(String markerId, LatLng position) {
218-
showMessage('Marker drag, position: $position markerId: $markerId');
219+
if (_displayMarkerUpdates) {
220+
setState(() {
221+
_latestMarkerDragUpdate =
222+
'Dragging\nMarker: $markerId\nLat: ${position.latitude.toStringAsFixed(4)}\nLng: ${position.longitude.toStringAsFixed(4)}';
223+
});
224+
}
219225
}
220226

221227
void _onMarkerDragStart(String markerId, LatLng position) {
222-
showMessage('Marker drag, position: $position markerId: $markerId');
228+
if (_displayMarkerUpdates) {
229+
setState(() {
230+
_latestMarkerDragUpdate =
231+
'Drag started\nMarker: $markerId\nLat: ${position.latitude.toStringAsFixed(4)}\nLng: ${position.longitude.toStringAsFixed(4)}';
232+
});
233+
}
223234
}
224235

225236
void _onMarkerDragEnd(String markerId, LatLng position) {
226-
showMessage('Marker drag, position: $position markerId: $markerId');
237+
if (_displayMarkerUpdates) {
238+
setState(() {
239+
_latestMarkerDragUpdate =
240+
'Drag ended\nMarker: $markerId\nLat: ${position.latitude.toStringAsFixed(4)}\nLng: ${position.longitude.toStringAsFixed(4)}';
241+
});
242+
}
227243
final Marker marker = _markers.firstWhere(
228244
(Marker marker) => marker.markerId == markerId,
229245
);
@@ -252,17 +268,47 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
252268
child: Column(
253269
children: <Widget>[
254270
Expanded(
255-
child: GoogleMapsNavigationView(
256-
onViewCreated: _onViewCreated,
257-
initialNavigationUIEnabledPreference:
258-
NavigationUIEnabledPreference.disabled,
259-
onMarkerClicked: _onMarkerClicked,
260-
onMarkerDrag: _onMarkerDrag,
261-
onMarkerDragStart: _onMarkerDragStart,
262-
onMarkerDragEnd: _onMarkerDragEnd,
263-
onMarkerInfoWindowClicked: _onMarkerInfoWindowClicked,
264-
onMarkerInfoWindowClosed: _onMarkerInfoWindowClosed,
265-
onMarkerInfoWindowLongClicked: _onMarkerInfoWindowLongClicked,
271+
child: Stack(
272+
children: <Widget>[
273+
GoogleMapsNavigationView(
274+
onViewCreated: _onViewCreated,
275+
initialNavigationUIEnabledPreference:
276+
NavigationUIEnabledPreference.disabled,
277+
onMarkerClicked: _onMarkerClicked,
278+
onMarkerDrag: _onMarkerDrag,
279+
onMarkerDragStart: _onMarkerDragStart,
280+
onMarkerDragEnd: _onMarkerDragEnd,
281+
onMarkerInfoWindowClicked: _onMarkerInfoWindowClicked,
282+
onMarkerInfoWindowClosed: _onMarkerInfoWindowClosed,
283+
onMarkerInfoWindowLongClicked: _onMarkerInfoWindowLongClicked,
284+
),
285+
if (_displayMarkerUpdates && _latestMarkerDragUpdate.isNotEmpty)
286+
Positioned(
287+
top: 10,
288+
left: 10,
289+
child: Container(
290+
padding: const EdgeInsets.all(12),
291+
decoration: BoxDecoration(
292+
color: Colors.white,
293+
borderRadius: BorderRadius.circular(8),
294+
boxShadow: <BoxShadow>[
295+
BoxShadow(
296+
color: Colors.black.withOpacity(0.2),
297+
blurRadius: 6,
298+
offset: const Offset(0, 2),
299+
),
300+
],
301+
),
302+
child: Text(
303+
_latestMarkerDragUpdate,
304+
style: const TextStyle(
305+
fontSize: 13,
306+
fontWeight: FontWeight.w500,
307+
),
308+
),
309+
),
310+
),
311+
],
266312
),
267313
),
268314
const SizedBox(height: 10),

example/lib/pages/navigation.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,16 +690,26 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
690690

691691
Marker? _newWaypointMarker;
692692
final List<Marker> _destinationWaypointMarkers = <Marker>[];
693+
PointOfInterest? _lastClickedPoi;
693694

694-
MarkerOptions _buildNewWaypointMarkerOptions(LatLng target) {
695+
MarkerOptions _buildNewWaypointMarkerOptions(
696+
LatLng target, {
697+
String? poiName,
698+
}) {
695699
return MarkerOptions(
696-
infoWindow: const InfoWindow(title: 'Destination'),
700+
infoWindow: InfoWindow(title: poiName ?? 'Destination'),
697701
position: LatLng(latitude: target.latitude, longitude: target.longitude),
698702
);
699703
}
700704

701-
Future<void> _updateNewWaypointMarker(LatLng target) async {
702-
final MarkerOptions markerOptions = _buildNewWaypointMarkerOptions(target);
705+
Future<void> _updateNewWaypointMarker(
706+
LatLng target, {
707+
String? poiName,
708+
}) async {
709+
final MarkerOptions markerOptions = _buildNewWaypointMarkerOptions(
710+
target,
711+
poiName: poiName,
712+
);
703713
if (_newWaypointMarker == null) {
704714
// Add new marker.
705715
final List<Marker?> addedMarkers = await _navigationViewController!
@@ -749,18 +759,29 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
749759
}
750760

751761
Future<void> _onMapClicked(LatLng location) async {
762+
_lastClickedPoi = null; // Clear POI info when map is clicked
752763
await _updateNewWaypointMarker(location);
753764
}
754765

766+
Future<void> _onPoiClicked(PointOfInterest poi) async {
767+
_lastClickedPoi = poi; // Store POI info for waypoint title
768+
await _updateNewWaypointMarker(poi.latLng, poiName: poi.name);
769+
}
770+
755771
Future<void> _addWaypoint() async {
756772
if (_newWaypointMarker != null) {
757773
setState(() {
758774
_validRoute = false;
759775
});
760776
_nextWaypointIndex += 1;
777+
// Use POI name if available, otherwise use generic waypoint title
778+
final String waypointTitle =
779+
_lastClickedPoi != null
780+
? '${_lastClickedPoi!.name} (Waypoint $_nextWaypointIndex)'
781+
: 'Waypoint $_nextWaypointIndex';
761782
_waypoints.add(
762783
NavigationWaypoint.withLatLngTarget(
763-
title: 'Waypoint $_nextWaypointIndex',
784+
title: waypointTitle,
764785
target: LatLng(
765786
latitude: _newWaypointMarker!.options.position.latitude,
766787
longitude: _newWaypointMarker!.options.position.longitude,
@@ -1226,6 +1247,7 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
12261247
onViewCreated: _onViewCreated,
12271248
onMapClicked: _onMapClicked,
12281249
onMapLongClicked: _onMapClicked,
1250+
onPoiClicked: _onPoiClicked,
12291251
onRecenterButtonClicked:
12301252
_onRecenterButtonClickedEvent,
12311253
onNavigationUIEnabledChanged:

example/lib/pages/poi_click.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class _PoiClickPageState extends ExamplePageState<PoiClickPage> {
7575
),
7676
Padding(
7777
padding: const EdgeInsets.only(top: 4),
78-
child: Text(_lastClickedPoi!.placeId),
78+
child: Text(_lastClickedPoi!.placeID),
7979
),
8080
],
8181
),

ios/google_navigation_flutter/Sources/google_navigation_flutter/GoogleMapsNavigationView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ extension GoogleMapsNavigationView: GMSMapViewDelegate {
10681068
getViewEventApi()?.onPoiClick(
10691069
viewId: _viewId!,
10701070
pointOfInterest: PointOfInterestDto(
1071-
placeId: placeID,
1071+
placeID: placeID,
10721072
name: name,
10731073
latLng: LatLngDto(latitude: location.latitude, longitude: location.longitude)
10741074
),

ios/google_navigation_flutter/Sources/google_navigation_flutter/messages.g.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,27 +906,27 @@ struct MarkerAnchorDto: Hashable {
906906
struct PointOfInterestDto: Hashable {
907907
/// The Place ID of this POI, as defined in the Places SDK.
908908
/// This can be used to retrieve additional information about the place.
909-
var placeId: String
909+
var placeID: String
910910
/// The name of the POI (e.g., "Central Park", "City Hall").
911911
var name: String
912912
/// The geographical coordinates of the POI.
913913
var latLng: LatLngDto
914914

915915
// swift-format-ignore: AlwaysUseLowerCamelCase
916916
static func fromList(_ pigeonVar_list: [Any?]) -> PointOfInterestDto? {
917-
let placeId = pigeonVar_list[0] as! String
917+
let placeID = pigeonVar_list[0] as! String
918918
let name = pigeonVar_list[1] as! String
919919
let latLng = pigeonVar_list[2] as! LatLngDto
920920

921921
return PointOfInterestDto(
922-
placeId: placeId,
922+
placeID: placeID,
923923
name: name,
924924
latLng: latLng
925925
)
926926
}
927927
func toList() -> [Any?] {
928928
return [
929-
placeId,
929+
placeID,
930930
name,
931931
latLng,
932932
]

lib/src/method_channel/auto_view_api.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class AutoMapViewAPIImpl {
2929
final StreamController<_AutoEventWrapper> _autoEventStreamController =
3030
StreamController<_AutoEventWrapper>.broadcast();
3131

32+
/// Provides access to the auto event stream controller for testing purposes.
33+
/// This allows test subclasses to access the parent's stream controller.
34+
@visibleForTesting
35+
StreamController<Object> get autoEventStreamControllerForTesting =>
36+
_autoEventStreamController;
37+
3238
/// Keep track of marker count, used to generate marker ID's.
3339
int _markerCounter = 0;
3440
String _createMarkerId() {

lib/src/method_channel/convert/convert.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export 'navigation.dart';
2424
export 'navigation_display_options.dart';
2525
export 'navigation_force_night_mode.dart';
2626
export 'navigation_routing_options.dart';
27+
export 'navigation_waypoint.dart';
2728
export 'navinfo.dart';
2829
export 'pattern.dart';
2930
export 'poi.dart';

0 commit comments

Comments
 (0)