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

Commit ce18897

Browse files
RafaOiskakaushik
authored andcommitted
Add support for map tapping (#985)
1 parent beccd5f commit ce18897

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static Object toJson(String markerId) {
127127
return data;
128128
}
129129

130-
private static Object toJson(LatLng latLng) {
130+
static Object toJson(LatLng latLng) {
131131
return Arrays.asList(latLng.latitude, latLng.longitude);
132132
}
133133

packages/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
@@ -26,6 +26,7 @@
2626
import com.google.android.gms.maps.MapView;
2727
import com.google.android.gms.maps.OnMapReadyCallback;
2828
import com.google.android.gms.maps.model.CameraPosition;
29+
import com.google.android.gms.maps.model.LatLng;
2930
import com.google.android.gms.maps.model.LatLngBounds;
3031
import com.google.android.gms.maps.model.Marker;
3132
import io.flutter.plugin.common.MethodCall;
@@ -50,6 +51,7 @@ final class GoogleMapController
5051
GoogleMapOptionsSink,
5152
MethodChannel.MethodCallHandler,
5253
OnMapReadyCallback,
54+
GoogleMap.OnMapClickListener,
5355
PlatformView {
5456

5557
private static final String TAG = "GoogleMapController";
@@ -155,6 +157,7 @@ public void onMapReady(GoogleMap googleMap) {
155157
googleMap.setOnCameraMoveListener(this);
156158
googleMap.setOnCameraIdleListener(this);
157159
googleMap.setOnMarkerClickListener(this);
160+
googleMap.setOnMapClickListener(this);
158161
updateMyLocationEnabled();
159162
markersController.setGoogleMap(googleMap);
160163
updateInitialMarkers();
@@ -241,6 +244,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
241244
}
242245
}
243246

247+
@Override
248+
public void onMapClick(LatLng latLng) {
249+
final Map<String, Object> arguments = new HashMap<>(2);
250+
arguments.put("position", Convert.toJson(latLng));
251+
methodChannel.invokeMethod("map#onTap", arguments);
252+
}
253+
244254
@Override
245255
public void onCameraMoveStarted(int reason) {
246256
final Map<String, Object> arguments = new HashMap<>(2);

packages/google_maps_flutter/ios/Classes/GoogleMapController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma mark - Conversion of JSON-like values sent via platform channels. Forward declarations.
99

1010
static NSDictionary* PositionToJson(GMSCameraPosition* position);
11+
static NSArray* LocationToJson(CLLocationCoordinate2D position);
1112
static GMSCameraPosition* ToOptionalCameraPosition(NSDictionary* json);
1213
static GMSCoordinateBounds* ToOptionalBounds(NSArray* json);
1314
static GMSCameraUpdate* ToCameraUpdate(NSArray* data);
@@ -257,6 +258,10 @@ - (void)mapView:(GMSMapView*)mapView didTapInfoWindowOfMarker:(GMSMarker*)marker
257258
[_markersController onInfoWindowTap:markerId];
258259
}
259260

261+
- (void)mapView:(GMSMapView*)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
262+
[_channel invokeMethod:@"map#onTap" arguments:@{@"position" : LocationToJson(coordinate)}];
263+
}
264+
260265
@end
261266

262267
#pragma mark - Implementations of JSON conversion functions.

packages/google_maps_flutter/lib/src/controller.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class GoogleMapController {
6363
case 'infoWindow#onTap':
6464
_googleMapState.onInfoWindowTap(call.arguments['markerId']);
6565
break;
66+
case 'map#onTap':
67+
_googleMapState.onTap(LatLng._fromJson(call.arguments['position']));
68+
break;
6669
default:
6770
throw MissingPluginException();
6871
}

packages/google_maps_flutter/lib/src/google_map.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class GoogleMap extends StatefulWidget {
3333
this.onCameraMoveStarted,
3434
this.onCameraMove,
3535
this.onCameraIdle,
36+
this.onTap,
3637
}) : assert(initialCameraPosition != null),
3738
super(key: key);
3839

@@ -91,6 +92,9 @@ class GoogleMap extends StatefulWidget {
9192
/// animations and the user has stopped interacting with the map.
9293
final VoidCallback onCameraIdle;
9394

95+
/// Called every time a [GoogleMap] is tapped.
96+
final ArgumentCallback<LatLng> onTap;
97+
9498
/// True if a "My Location" layer should be shown on the map.
9599
///
96100
/// This layer includes a location indicator at the current device location,
@@ -223,6 +227,11 @@ class _GoogleMapState extends State<GoogleMap> {
223227
final MarkerId markerId = MarkerId(markerIdParam);
224228
_markers[markerId].infoWindow.onTap();
225229
}
230+
231+
void onTap(LatLng position) {
232+
assert(position != null);
233+
widget.onTap(position);
234+
}
226235
}
227236

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

0 commit comments

Comments
 (0)