Skip to content

Commit 17ea7f3

Browse files
committed
google maps implement OnPoiClickListener
1 parent 1fc3d92 commit 17ea7f3

15 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
## 2.0.9
3+
4+
* Add GoogleMap.OnPoiClickListener
5+
16
## 2.0.8
27

38
* Mark iOS arm64 simulators as unsupported.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.android.gms.maps.model.LatLngBounds;
3131
import com.google.android.gms.maps.model.MapStyleOptions;
3232
import com.google.android.gms.maps.model.Marker;
33+
import com.google.android.gms.maps.model.PointOfInterest;
3334
import com.google.android.gms.maps.model.Polygon;
3435
import com.google.android.gms.maps.model.Polyline;
3536
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
@@ -433,6 +434,14 @@ public void onMapLongClick(LatLng latLng) {
433434
methodChannel.invokeMethod("map#onLongPress", arguments);
434435
}
435436

437+
public void onPoiClick(PointOfInterest poi) {
438+
final Map<String, Object> arguments = new HashMap<>(2);
439+
arguments.put("position", Convert.latLngToJson(poi.latLng));
440+
arguments.put("name", poi.name);
441+
arguments.put("placeId", poi.placeId);
442+
methodChannel.invokeMethod("map#onPoiClick", arguments);
443+
}
444+
436445
@Override
437446
public void onCameraMoveStarted(int reason) {
438447
final Map<String, Object> arguments = new HashMap<>(2);
@@ -518,6 +527,7 @@ private void setGoogleMapListener(@Nullable GoogleMapListener listener) {
518527
googleMap.setOnCircleClickListener(listener);
519528
googleMap.setOnMapClickListener(listener);
520529
googleMap.setOnMapLongClickListener(listener);
530+
googleMap.setOnPoiClickListener(listener);
521531
}
522532

523533
// @Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package io.flutter.plugins.googlemaps;
66

77
import com.google.android.gms.maps.GoogleMap;
8+
// import com.google.android.gms.maps.model.PointOfInterest;
89

910
interface GoogleMapListener
1011
extends GoogleMap.OnCameraIdleListener,
@@ -17,4 +18,5 @@ interface GoogleMapListener
1718
GoogleMap.OnCircleClickListener,
1819
GoogleMap.OnMapClickListener,
1920
GoogleMap.OnMapLongClickListener,
20-
GoogleMap.OnMarkerDragListener {}
21+
GoogleMap.OnMarkerDragListener,
22+
GoogleMap.OnPoiClickListener {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ - (void)mapView:(GMSMapView*)mapView didLongPressAtCoordinate:(CLLocationCoordin
532532
[_channel invokeMethod:@"map#onLongPress" arguments:@{@"position" : LocationToJson(coordinate)}];
533533
}
534534

535+
- (void)mapView:(GMSMapView*)mapView didTapPOIWithPlaceID:(NSString *)placeID name:(NSString *)name location:(CLLocationCoordinate2D)location {
536+
[_channel invokeMethod:@"map#onPoiClick" arguments:@{@"position" : LocationToJson(coordinate), @"name" : name, @"placeId" : placeID, }];
537+
}
538+
535539
@end
536540

537541
#pragma mark - Implementations of JSON conversion functions.

‎packages/google_maps_flutter/google_maps_flutter/lib/google_maps_flutter.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export 'package:google_maps_flutter_platform_interface/google_maps_flutter_platf
3838
MarkerId,
3939
MinMaxZoomPreference,
4040
PatternItem,
41+
PointOfInterest,
4142
Polygon,
4243
PolygonId,
4344
Polyline,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class GoogleMapController {
8888
.listen((MapTapEvent e) => _googleMapState.onTap(e.position));
8989
GoogleMapsFlutterPlatform.instance.onLongPress(mapId: mapId).listen(
9090
(MapLongPressEvent e) => _googleMapState.onLongPress(e.position));
91+
GoogleMapsFlutterPlatform.instance.onPoiClick(mapId: mapId).listen(
92+
(MapPoiClickEvent e) => _googleMapState.onPoiClick(e.value));
9193
}
9294

9395
/// Updates configuration options of the map user interface.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class GoogleMap extends StatefulWidget {
7777
this.onCameraIdle,
7878
this.onTap,
7979
this.onLongPress,
80+
this.onPoiClick,
8081
}) : assert(initialCameraPosition != null),
8182
super(key: key);
8283

@@ -173,6 +174,9 @@ class GoogleMap extends StatefulWidget {
173174
/// Called every time a [GoogleMap] is long pressed.
174175
final ArgumentCallback<LatLng>? onLongPress;
175176

177+
/// Called every time a [PointOfInterest] is clicked.
178+
final ArgumentCallback<PointOfInterest>? onPoiClick;
179+
176180
/// True if a "My Location" layer should be shown on the map.
177181
///
178182
/// This layer includes a location indicator at the current device location,
@@ -443,6 +447,14 @@ class _GoogleMapState extends State<GoogleMap> {
443447
onLongPress(position);
444448
}
445449
}
450+
451+
void onPoiClick(PointOfInterest poi) {
452+
assert(poi != null);
453+
final ArgumentCallback<PointOfInterest>? onPoiClick = widget.onPoiClick;
454+
if (onPoiClick != null) {
455+
onPoiClick(poi);
456+
}
457+
}
446458
}
447459

448460
/// Configuration options for the GoogleMaps user interface.

‎packages/google_maps_flutter/google_maps_flutter/test/map_creation_test.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ class TestGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
259259
return mapEventStreamController.stream.whereType<MapLongPressEvent>();
260260
}
261261

262+
@override
263+
Stream<MapPoiClickEvent> onPoiClick({required int mapId}) {
264+
return mapEventStreamController.stream.whereType<MapPoiClickEvent>();
265+
}
266+
267+
262268
@override
263269
void dispose({required int mapId}) {
264270
disposed = true;

‎packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/events/map_event.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
66
import 'package:google_maps_flutter_platform_interface/src/method_channel/method_channel_google_maps_flutter.dart';
77

8+
import '../types/point_of_interest.dart';
9+
810
/// Generic Event coming from the native side of Maps.
911
///
1012
/// All MapEvents contain the `mapId` that originated the event. This should
@@ -151,3 +153,11 @@ class MapLongPressEvent extends _PositionedMapEvent<void> {
151153
/// The `position` of this event is the LatLng where the Map was long pressed.
152154
MapLongPressEvent(int mapId, LatLng position) : super(mapId, position, null);
153155
}
156+
157+
/// An event fired when a Map is long pressed.
158+
class MapPoiClickEvent extends MapEvent<PointOfInterest> {
159+
160+
/// Build an MapTap Event triggered from the map represented by `mapId`.
161+
MapPoiClickEvent(int mapId, PointOfInterest poi) : super(mapId, poi);
162+
}
163+

‎packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/method_channel/method_channel_google_maps_flutter.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform {
154154
return _events(mapId).whereType<MapLongPressEvent>();
155155
}
156156

157+
@override
158+
Stream<MapPoiClickEvent> onPoiClick({required int mapId}) {
159+
return _events(mapId).whereType<MapPoiClickEvent>();
160+
}
161+
157162
Future<dynamic> _handleMethodCall(MethodCall call, int mapId) async {
158163
switch (call.method) {
159164
case 'camera#onMoveStarted':
@@ -217,6 +222,15 @@ class MethodChannelGoogleMapsFlutter extends GoogleMapsFlutterPlatform {
217222
LatLng.fromJson(call.arguments['position'])!,
218223
));
219224
break;
225+
case 'map#onPoiClick':
226+
_mapEventStreamController.add(MapPoiClickEvent(
227+
mapId,
228+
PointOfInterest(
229+
LatLng.fromJson(call.arguments['position'])!,
230+
call.arguments['name'],
231+
call.arguments['placeId'],),
232+
));
233+
break;
220234
case 'tileOverlay#getTile':
221235
final Map<TileOverlayId, TileOverlay>? tileOverlaysForThisMap =
222236
_tileOverlays[mapId];

0 commit comments

Comments
 (0)