Skip to content

Commit 7707384

Browse files
eliezedeckclaude
andcommitted
✨ Add Point of Interest (POI) tap callback support
This implements the onPoiTap callback for Google Maps Flutter, addressing issue #60695. The implementation follows the reviewer's architectural pattern by providing a lightweight callback that includes: - PointOfInterestId: wraps the Google Place ID (always available) - LatLng position: geographic coordinates (always available) - String? name: display name (available on Android/iOS, null on web) Changes: - platform_interface: Add PointOfInterest, PointOfInterestId, PoiTapEvent - google_maps_flutter: Add onPoiTap callback to GoogleMap widget - google_maps_flutter_android: Implement OnPoiClickListener - google_maps_flutter_ios: Implement didTapPOIWithPlaceID delegate This addresses the cost concerns for web implementations by not forcing automatic Places API calls - developers can optionally fetch additional details using the placeId. Resolves: flutter/flutter#60695 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent a6542ce commit 7707384

19 files changed

Lines changed: 771 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ class GoogleMapController {
133133
.onClusterTap(mapId: mapId)
134134
.listen((ClusterTapEvent e) => _googleMapState.onClusterTap(e.value)),
135135
);
136+
_streamSubscriptions.add(
137+
GoogleMapsFlutterPlatform.instance
138+
.onPoiTap(mapId: mapId)
139+
.listen((PoiTapEvent e) => _googleMapState.onPoiTap(e.value)),
140+
);
136141
}
137142

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

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class GoogleMap extends StatefulWidget {
143143
this.onCameraIdle,
144144
this.onTap,
145145
this.onLongPress,
146+
this.onPoiTap,
146147
this.markerType = GoogleMapMarkerType.marker,
147148
String? mapId,
148149
@Deprecated('cloudMapId is deprecated. Use mapId instead.')
@@ -307,6 +308,30 @@ class GoogleMap extends StatefulWidget {
307308
/// Called every time a [GoogleMap] is long pressed.
308309
final ArgumentCallback<LatLng>? onLongPress;
309310

311+
/// Called every time a Point of Interest (POI) is tapped on the map.
312+
///
313+
/// POIs are landmarks, businesses, and other notable locations that appear
314+
/// on the Google Map.
315+
///
316+
/// The callback receives a [PointOfInterest] object containing:
317+
/// - [PointOfInterest.poiId]: The unique Place ID (always available)
318+
/// - [PointOfInterest.position]: The geographic coordinates (always available)
319+
/// - [PointOfInterest.name]: The display name (available on Android/iOS, null on web)
320+
///
321+
/// To fetch additional details about the POI (such as address, phone number,
322+
/// opening hours), use the [PointOfInterest.poiId] with the Google Places API.
323+
///
324+
/// Example:
325+
/// ```dart
326+
/// GoogleMap(
327+
/// onPoiTap: (poi) {
328+
/// print('Tapped: ${poi.name ?? 'Unknown'} at ${poi.position}');
329+
/// // Use poi.poiId.value with Places API for more details
330+
/// },
331+
/// )
332+
/// ```
333+
final ArgumentCallback<PointOfInterest>? onPoiTap;
334+
310335
/// True if a "My Location" layer should be shown on the map.
311336
///
312337
/// This layer includes a location indicator at the current device location,
@@ -734,6 +759,13 @@ class _GoogleMapState extends State<GoogleMap> {
734759
onClusterTap(cluster);
735760
}
736761
}
762+
763+
void onPoiTap(PointOfInterest poi) {
764+
final ArgumentCallback<PointOfInterest>? onPoiTap = widget.onPoiTap;
765+
if (onPoiTap != null) {
766+
onPoiTap(poi);
767+
}
768+
}
737769
}
738770

739771
/// Builds a [MapConfiguration] from the given [map].

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.android.gms.maps.model.MapCapabilities;
3939
import com.google.android.gms.maps.model.MapStyleOptions;
4040
import com.google.android.gms.maps.model.Marker;
41+
import com.google.android.gms.maps.model.PointOfInterest;
4142
import com.google.android.gms.maps.model.Polygon;
4243
import com.google.android.gms.maps.model.Polyline;
4344
import com.google.android.gms.maps.model.TileOverlay;
@@ -51,7 +52,9 @@
5152
import io.flutter.plugins.googlemaps.Messages.MapsApi;
5253
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
5354
import io.flutter.plugins.googlemaps.Messages.MapsInspectorApi;
55+
import io.flutter.plugins.googlemaps.Messages.PlatformLatLng;
5456
import io.flutter.plugins.googlemaps.Messages.PlatformMarkerType;
57+
import io.flutter.plugins.googlemaps.Messages.PlatformPointOfInterest;
5558
import java.io.ByteArrayOutputStream;
5659
import java.util.ArrayList;
5760
import java.util.List;
@@ -387,6 +390,21 @@ public void onGroundOverlayClick(@NonNull GroundOverlay groundOverlay) {
387390
groundOverlaysController.onGroundOverlayTap(groundOverlay.getId());
388391
}
389392

393+
@Override
394+
public void onPoiClick(@NonNull PointOfInterest poi) {
395+
PlatformPointOfInterest platformPoi =
396+
new PlatformPointOfInterest.Builder()
397+
.setPlaceId(poi.placeId)
398+
.setPosition(
399+
new PlatformLatLng.Builder()
400+
.setLatitude(poi.latLng.latitude)
401+
.setLongitude(poi.latLng.longitude)
402+
.build())
403+
.setName(poi.name)
404+
.build();
405+
flutterApi.onPoiTap(platformPoi, new NoOpVoidResult());
406+
}
407+
390408
@Override
391409
public void dispose() {
392410
if (disposed) {
@@ -420,6 +438,7 @@ private void setGoogleMapListener(@Nullable GoogleMapListener listener) {
420438
googleMap.setOnMapClickListener(listener);
421439
googleMap.setOnMapLongClickListener(listener);
422440
googleMap.setOnGroundOverlayClickListener(listener);
441+
googleMap.setOnPoiClickListener(listener);
423442
}
424443

425444
@VisibleForTesting

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ interface GoogleMapListener
1818
GoogleMap.OnMapClickListener,
1919
GoogleMap.OnMapLongClickListener,
2020
GoogleMap.OnMarkerDragListener,
21-
GoogleMap.OnGroundOverlayClickListener {}
21+
GoogleMap.OnGroundOverlayClickListener,
22+
GoogleMap.OnPoiClickListener {}

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

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4275,6 +4275,129 @@ ArrayList<Object> toList() {
42754275
}
42764276
}
42774277

4278+
/**
4279+
* Pigeon equivalent of PointOfInterest.
4280+
*
4281+
* <p>Represents a Point of Interest (POI) that was tapped on the map. POIs are landmarks,
4282+
* businesses, and other notable locations.
4283+
*
4284+
* <p>Generated class from Pigeon that represents data sent in messages.
4285+
*/
4286+
public static final class PlatformPointOfInterest {
4287+
private @NonNull String placeId;
4288+
4289+
public @NonNull String getPlaceId() {
4290+
return placeId;
4291+
}
4292+
4293+
public void setPlaceId(@NonNull String setterArg) {
4294+
if (setterArg == null) {
4295+
throw new IllegalStateException("Nonnull field \"placeId\" is null.");
4296+
}
4297+
this.placeId = setterArg;
4298+
}
4299+
4300+
private @NonNull PlatformLatLng position;
4301+
4302+
public @NonNull PlatformLatLng getPosition() {
4303+
return position;
4304+
}
4305+
4306+
public void setPosition(@NonNull PlatformLatLng setterArg) {
4307+
if (setterArg == null) {
4308+
throw new IllegalStateException("Nonnull field \"position\" is null.");
4309+
}
4310+
this.position = setterArg;
4311+
}
4312+
4313+
private @Nullable String name;
4314+
4315+
public @Nullable String getName() {
4316+
return name;
4317+
}
4318+
4319+
public void setName(@Nullable String setterArg) {
4320+
this.name = setterArg;
4321+
}
4322+
4323+
/** Constructor is non-public to enforce null safety; use Builder. */
4324+
PlatformPointOfInterest() {}
4325+
4326+
@Override
4327+
public boolean equals(Object o) {
4328+
if (this == o) {
4329+
return true;
4330+
}
4331+
if (o == null || getClass() != o.getClass()) {
4332+
return false;
4333+
}
4334+
PlatformPointOfInterest that = (PlatformPointOfInterest) o;
4335+
return placeId.equals(that.placeId)
4336+
&& position.equals(that.position)
4337+
&& Objects.equals(name, that.name);
4338+
}
4339+
4340+
@Override
4341+
public int hashCode() {
4342+
return Objects.hash(placeId, position, name);
4343+
}
4344+
4345+
public static final class Builder {
4346+
4347+
private @Nullable String placeId;
4348+
4349+
@CanIgnoreReturnValue
4350+
public @NonNull Builder setPlaceId(@NonNull String setterArg) {
4351+
this.placeId = setterArg;
4352+
return this;
4353+
}
4354+
4355+
private @Nullable PlatformLatLng position;
4356+
4357+
@CanIgnoreReturnValue
4358+
public @NonNull Builder setPosition(@NonNull PlatformLatLng setterArg) {
4359+
this.position = setterArg;
4360+
return this;
4361+
}
4362+
4363+
private @Nullable String name;
4364+
4365+
@CanIgnoreReturnValue
4366+
public @NonNull Builder setName(@Nullable String setterArg) {
4367+
this.name = setterArg;
4368+
return this;
4369+
}
4370+
4371+
public @NonNull PlatformPointOfInterest build() {
4372+
PlatformPointOfInterest pigeonReturn = new PlatformPointOfInterest();
4373+
pigeonReturn.setPlaceId(placeId);
4374+
pigeonReturn.setPosition(position);
4375+
pigeonReturn.setName(name);
4376+
return pigeonReturn;
4377+
}
4378+
}
4379+
4380+
@NonNull
4381+
ArrayList<Object> toList() {
4382+
ArrayList<Object> toListResult = new ArrayList<>(3);
4383+
toListResult.add(placeId);
4384+
toListResult.add(position);
4385+
toListResult.add(name);
4386+
return toListResult;
4387+
}
4388+
4389+
static @NonNull PlatformPointOfInterest fromList(@NonNull ArrayList<Object> pigeonVar_list) {
4390+
PlatformPointOfInterest pigeonResult = new PlatformPointOfInterest();
4391+
Object placeId = pigeonVar_list.get(0);
4392+
pigeonResult.setPlaceId((String) placeId);
4393+
Object position = pigeonVar_list.get(1);
4394+
pigeonResult.setPosition((PlatformLatLng) position);
4395+
Object name = pigeonVar_list.get(2);
4396+
pigeonResult.setName((String) name);
4397+
return pigeonResult;
4398+
}
4399+
}
4400+
42784401
/**
42794402
* Pigeon equivalent of the GroundOverlay class.
42804403
*
@@ -7023,6 +7146,8 @@ protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
70237146
return PlatformBitmapBytesMap.fromList((ArrayList<Object>) readValue(buffer));
70247147
case (byte) 180:
70257148
return PlatformBitmapPinConfig.fromList((ArrayList<Object>) readValue(buffer));
7149+
case (byte) 181:
7150+
return PlatformPointOfInterest.fromList((ArrayList<Object>) readValue(buffer));
70267151
default:
70277152
return super.readValueOfType(type, buffer);
70287153
}
@@ -7186,6 +7311,9 @@ protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
71867311
} else if (value instanceof PlatformBitmapPinConfig) {
71877312
stream.write(180);
71887313
writeValue(stream, ((PlatformBitmapPinConfig) value).toList());
7314+
} else if (value instanceof PlatformPointOfInterest) {
7315+
stream.write(181);
7316+
writeValue(stream, ((PlatformPointOfInterest) value).toList());
71897317
} else {
71907318
super.writeValue(stream, value);
71917319
}
@@ -8350,6 +8478,34 @@ public void onGroundOverlayTap(@NonNull String groundOverlayIdArg, @NonNull Void
83508478
}
83518479
});
83528480
}
8481+
/**
8482+
* Called when a Point of Interest (POI) is tapped.
8483+
*
8484+
* <p>POIs are landmarks, businesses, and other notable locations on the map.
8485+
*/
8486+
public void onPoiTap(@NonNull PlatformPointOfInterest poiArg, @NonNull VoidResult result) {
8487+
final String channelName =
8488+
"dev.flutter.pigeon.google_maps_flutter_android.MapsCallbackApi.onPoiTap"
8489+
+ messageChannelSuffix;
8490+
BasicMessageChannel<Object> channel =
8491+
new BasicMessageChannel<>(binaryMessenger, channelName, getCodec());
8492+
channel.send(
8493+
new ArrayList<>(Collections.singletonList(poiArg)),
8494+
channelReply -> {
8495+
if (channelReply instanceof List) {
8496+
List<Object> listReply = (List<Object>) channelReply;
8497+
if (listReply.size() > 1) {
8498+
result.error(
8499+
new FlutterError(
8500+
(String) listReply.get(0), (String) listReply.get(1), listReply.get(2)));
8501+
} else {
8502+
result.success();
8503+
}
8504+
} else {
8505+
result.error(createConnectionError(channelName));
8506+
}
8507+
});
8508+
}
83538509
/** Called to get data for a map tile. */
83548510
public void getTileOverlayTile(
83558511
@NonNull String tileOverlayIdArg,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ class GoogleMapsFlutterAndroid extends GoogleMapsFlutterPlatform {
228228
return _events(mapId).whereType<ClusterTapEvent>();
229229
}
230230

231+
@override
232+
Stream<PoiTapEvent> onPoiTap({required int mapId}) {
233+
return _events(mapId).whereType<PoiTapEvent>();
234+
}
235+
231236
@override
232237
Future<void> updateMapConfiguration(
233238
MapConfiguration configuration, {
@@ -1324,6 +1329,20 @@ class HostMapMessageHandler implements MapsCallbackApi {
13241329
MapTapEvent(mapId, _latLngFromPlatformLatLng(position)),
13251330
);
13261331
}
1332+
1333+
@override
1334+
void onPoiTap(PlatformPointOfInterest poi) {
1335+
streamController.add(
1336+
PoiTapEvent(
1337+
mapId,
1338+
PointOfInterest(
1339+
poiId: PointOfInterestId(poi.placeId),
1340+
position: _latLngFromPlatformLatLng(poi.position),
1341+
name: poi.name,
1342+
),
1343+
),
1344+
);
1345+
}
13271346
}
13281347

13291348
LatLng _latLngFromPlatformLatLng(PlatformLatLng latLng) {

0 commit comments

Comments
 (0)