-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
google_map.dart
577 lines (502 loc) · 20.1 KB
/
google_map.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of '../google_maps_flutter.dart';
/// Callback method for when the map is ready to be used.
///
/// Pass to [GoogleMap.onMapCreated] to receive a [GoogleMapController] when the
/// map is created.
typedef MapCreatedCallback = void Function(GoogleMapController controller);
// This counter is used to provide a stable "constant" initialization id
// to the buildView function, so the web implementation can use it as a
// cache key. This needs to be provided from the outside, because web
// views seem to re-render much more often that mobile platform views.
int _nextMapCreationId = 0;
/// Error thrown when an unknown map object ID is provided to a method.
class UnknownMapObjectIdError extends Error {
/// Creates an assertion error with the provided [message].
UnknownMapObjectIdError(this.objectType, this.objectId, [this.context]);
/// The name of the map object whose ID is unknown.
final String objectType;
/// The unknown maps object ID.
final MapsObjectId<Object> objectId;
/// The context where the error occurred.
final String? context;
@override
String toString() {
if (context != null) {
return 'Unknown $objectType ID "${objectId.value}" in $context';
}
return 'Unknown $objectType ID "${objectId.value}"';
}
}
/// Android specific settings for [GoogleMap].
@Deprecated(
'See https://pub.dev/packages/google_maps_flutter_android#display-mode')
class AndroidGoogleMapsFlutter {
@Deprecated(
'See https://pub.dev/packages/google_maps_flutter_android#display-mode')
AndroidGoogleMapsFlutter._();
/// Whether to render [GoogleMap] with a [AndroidViewSurface] to build the Google Maps widget.
///
/// This implementation uses hybrid composition to render the Google Maps
/// Widget on Android. This comes at the cost of some performance on Android
/// versions below 10. See
/// https://flutter.dev/docs/development/platform-integration/platform-views#performance for more
/// information.
@Deprecated(
'See https://pub.dev/packages/google_maps_flutter_android#display-mode')
static bool get useAndroidViewSurface {
final GoogleMapsFlutterPlatform platform =
GoogleMapsFlutterPlatform.instance;
if (platform is GoogleMapsFlutterAndroid) {
return platform.useAndroidViewSurface;
}
return false;
}
/// Set whether to render [GoogleMap] with a [AndroidViewSurface] to build the Google Maps widget.
///
/// This implementation uses hybrid composition to render the Google Maps
/// Widget on Android. This comes at the cost of some performance on Android
/// versions below 10. See
/// https://flutter.dev/docs/development/platform-integration/platform-views#performance for more
/// information.
@Deprecated(
'See https://pub.dev/packages/google_maps_flutter_android#display-mode')
static set useAndroidViewSurface(bool useAndroidViewSurface) {
final GoogleMapsFlutterPlatform platform =
GoogleMapsFlutterPlatform.instance;
if (platform is GoogleMapsFlutterAndroid) {
platform.useAndroidViewSurface = useAndroidViewSurface;
}
}
}
/// A widget which displays a map with data obtained from the Google Maps service.
class GoogleMap extends StatefulWidget {
/// Creates a widget displaying data from Google Maps services.
///
/// [AssertionError] will be thrown if [initialCameraPosition] is null;
const GoogleMap({
super.key,
required this.initialCameraPosition,
this.style,
this.onMapCreated,
this.gestureRecognizers = const <Factory<OneSequenceGestureRecognizer>>{},
this.webGestureHandling,
this.compassEnabled = true,
this.mapToolbarEnabled = true,
this.cameraTargetBounds = CameraTargetBounds.unbounded,
this.mapType = MapType.normal,
this.minMaxZoomPreference = MinMaxZoomPreference.unbounded,
this.rotateGesturesEnabled = true,
this.scrollGesturesEnabled = true,
this.zoomControlsEnabled = true,
this.zoomGesturesEnabled = true,
this.liteModeEnabled = false,
this.tiltGesturesEnabled = true,
this.fortyFiveDegreeImageryEnabled = false,
this.myLocationEnabled = false,
this.myLocationButtonEnabled = true,
this.layoutDirection,
/// If no padding is specified default padding will be 0.
this.padding = EdgeInsets.zero,
this.indoorViewEnabled = false,
this.trafficEnabled = false,
this.buildingsEnabled = true,
this.markers = const <Marker>{},
this.polygons = const <Polygon>{},
this.polylines = const <Polyline>{},
this.circles = const <Circle>{},
this.onCameraMoveStarted,
this.tileOverlays = const <TileOverlay>{},
this.onCameraMove,
this.onCameraIdle,
this.onTap,
this.onLongPress,
this.cloudMapId,
});
/// Callback method for when the map is ready to be used.
///
/// Used to receive a [GoogleMapController] for this [GoogleMap].
final MapCreatedCallback? onMapCreated;
/// The initial position of the map's camera.
final CameraPosition initialCameraPosition;
/// The style for the map.
///
/// Set to null to clear any previous custom styling.
///
/// If problems were detected with the [mapStyle], including un-parsable
/// styling JSON, unrecognized feature type, unrecognized element type, or
/// invalid styler keys, the style is left unchanged, and the error can be
/// retrieved with [GoogleMapController.getStyleError].
///
/// The style string can be generated using the
/// [map style tool](https://mapstyle.withgoogle.com/).
final String? style;
/// True if the map should show a compass when rotated.
final bool compassEnabled;
/// True if the map should show a toolbar when you interact with the map. Android only.
final bool mapToolbarEnabled;
/// Geographical bounding box for the camera target.
final CameraTargetBounds cameraTargetBounds;
/// Type of map tiles to be rendered.
final MapType mapType;
/// The layout direction to use for the embedded view.
///
/// If this is null, the ambient [Directionality] is used instead. If there is
/// no ambient [Directionality], [TextDirection.ltr] is used.
final TextDirection? layoutDirection;
/// Preferred bounds for the camera zoom level.
///
/// Actual bounds depend on map data and device.
final MinMaxZoomPreference minMaxZoomPreference;
/// True if the map view should respond to rotate gestures.
final bool rotateGesturesEnabled;
/// True if the map view should respond to scroll gestures.
final bool scrollGesturesEnabled;
/// True if the map view should show zoom controls. This includes two buttons
/// to zoom in and zoom out. The default value is to show zoom controls.
///
/// This is only supported on Android. And this field is silently ignored on iOS.
final bool zoomControlsEnabled;
/// True if the map view should respond to zoom gestures.
final bool zoomGesturesEnabled;
/// True if the map view should be in lite mode. Android only.
///
/// See https://developers.google.com/maps/documentation/android-sdk/lite#overview_of_lite_mode for more details.
final bool liteModeEnabled;
/// True if the map view should respond to tilt gestures.
final bool tiltGesturesEnabled;
/// True if 45 degree imagery should be enabled. Web only.
final bool fortyFiveDegreeImageryEnabled;
/// Padding to be set on map. See https://developers.google.com/maps/documentation/android-sdk/map#map_padding for more details.
final EdgeInsets padding;
/// Markers to be placed on the map.
final Set<Marker> markers;
/// Polygons to be placed on the map.
final Set<Polygon> polygons;
/// Polylines to be placed on the map.
final Set<Polyline> polylines;
/// Circles to be placed on the map.
final Set<Circle> circles;
/// Tile overlays to be placed on the map.
final Set<TileOverlay> tileOverlays;
/// Called when the camera starts moving.
///
/// This can be initiated by the following:
/// 1. Non-gesture animation initiated in response to user actions.
/// For example: zoom buttons, my location button, or marker clicks.
/// 2. Programmatically initiated animation.
/// 3. Camera motion initiated in response to user gestures on the map.
/// For example: pan, tilt, pinch to zoom, or rotate.
final VoidCallback? onCameraMoveStarted;
/// Called repeatedly as the camera continues to move after an
/// onCameraMoveStarted call.
///
/// This may be called as often as once every frame and should
/// not perform expensive operations.
final CameraPositionCallback? onCameraMove;
/// Called when camera movement has ended, there are no pending
/// animations and the user has stopped interacting with the map.
final VoidCallback? onCameraIdle;
/// Called every time a [GoogleMap] is tapped.
final ArgumentCallback<LatLng>? onTap;
/// Called every time a [GoogleMap] is long pressed.
final ArgumentCallback<LatLng>? onLongPress;
/// True if a "My Location" layer should be shown on the map.
///
/// This layer includes a location indicator at the current device location,
/// as well as a My Location button.
/// * The indicator is a small blue dot if the device is stationary, or a
/// chevron if the device is moving.
/// * The My Location button animates to focus on the user's current location
/// if the user's location is currently known.
///
/// Enabling this feature requires adding location permissions to both native
/// platforms of your app.
/// * On Android add either
/// `<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />`
/// or `<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />`
/// to your `AndroidManifest.xml` file. `ACCESS_COARSE_LOCATION` returns a
/// location with an accuracy approximately equivalent to a city block, while
/// `ACCESS_FINE_LOCATION` returns as precise a location as possible, although
/// it consumes more battery power. You will also need to request these
/// permissions during run-time. If they are not granted, the My Location
/// feature will fail silently.
/// * On iOS add a `NSLocationWhenInUseUsageDescription` key to your
/// `Info.plist` file. This will automatically prompt the user for permissions
/// when the map tries to turn on the My Location layer.
final bool myLocationEnabled;
/// Enables or disables the my-location button.
///
/// The my-location button causes the camera to move such that the user's
/// location is in the center of the map. If the button is enabled, it is
/// only shown when the my-location layer is enabled.
///
/// By default, the my-location button is enabled (and hence shown when the
/// my-location layer is enabled).
///
/// See also:
/// * [myLocationEnabled] parameter.
final bool myLocationButtonEnabled;
/// Enables or disables the indoor view from the map
final bool indoorViewEnabled;
/// Enables or disables the traffic layer of the map
final bool trafficEnabled;
/// Enables or disables showing 3D buildings where available
final bool buildingsEnabled;
/// Which gestures should be consumed by the map.
///
/// It is possible for other gesture recognizers to be competing with the map on pointer
/// events, e.g if the map is inside a [ListView] the [ListView] will want to handle
/// vertical drags. The map will claim gestures that are recognized by any of the
/// recognizers on this list.
///
/// When this set is empty, the map will only handle pointer events for gestures that
/// were not claimed by any other gesture recognizer.
final Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers;
/// This setting controls how the API handles gestures on the map. Web only.
///
/// See [WebGestureHandling] for more details.
final WebGestureHandling? webGestureHandling;
/// Identifier that's associated with a specific cloud-based map style.
///
/// See https://developers.google.com/maps/documentation/get-map-id
/// for more details.
final String? cloudMapId;
/// Creates a [State] for this [GoogleMap].
@override
State createState() => _GoogleMapState();
}
class _GoogleMapState extends State<GoogleMap> {
final int _mapId = _nextMapCreationId++;
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
Map<MarkerId, Marker> _markers = <MarkerId, Marker>{};
Map<PolygonId, Polygon> _polygons = <PolygonId, Polygon>{};
Map<PolylineId, Polyline> _polylines = <PolylineId, Polyline>{};
Map<CircleId, Circle> _circles = <CircleId, Circle>{};
late MapConfiguration _mapConfiguration;
@override
Widget build(BuildContext context) {
return GoogleMapsFlutterPlatform.instance.buildViewWithConfiguration(
_mapId,
onPlatformViewCreated,
widgetConfiguration: MapWidgetConfiguration(
textDirection: widget.layoutDirection ??
Directionality.maybeOf(context) ??
TextDirection.ltr,
initialCameraPosition: widget.initialCameraPosition,
gestureRecognizers: widget.gestureRecognizers,
),
mapObjects: MapObjects(
markers: widget.markers,
polygons: widget.polygons,
polylines: widget.polylines,
circles: widget.circles,
),
mapConfiguration: _mapConfiguration,
);
}
@override
void initState() {
super.initState();
_mapConfiguration = _configurationFromMapWidget(widget);
_markers = keyByMarkerId(widget.markers);
_polygons = keyByPolygonId(widget.polygons);
_polylines = keyByPolylineId(widget.polylines);
_circles = keyByCircleId(widget.circles);
}
@override
void dispose() {
_disposeController();
super.dispose();
}
Future<void> _disposeController() async {
final GoogleMapController controller = await _controller.future;
controller.dispose();
}
@override
void didUpdateWidget(GoogleMap oldWidget) {
super.didUpdateWidget(oldWidget);
_updateOptions();
_updateMarkers();
_updatePolygons();
_updatePolylines();
_updateCircles();
_updateTileOverlays();
}
Future<void> _updateOptions() async {
final MapConfiguration newConfig = _configurationFromMapWidget(widget);
final MapConfiguration updates = newConfig.diffFrom(_mapConfiguration);
if (updates.isEmpty) {
return;
}
final GoogleMapController controller = await _controller.future;
unawaited(controller._updateMapConfiguration(updates));
_mapConfiguration = newConfig;
}
Future<void> _updateMarkers() async {
final GoogleMapController controller = await _controller.future;
unawaited(controller._updateMarkers(
MarkerUpdates.from(_markers.values.toSet(), widget.markers)));
_markers = keyByMarkerId(widget.markers);
}
Future<void> _updatePolygons() async {
final GoogleMapController controller = await _controller.future;
unawaited(controller._updatePolygons(
PolygonUpdates.from(_polygons.values.toSet(), widget.polygons)));
_polygons = keyByPolygonId(widget.polygons);
}
Future<void> _updatePolylines() async {
final GoogleMapController controller = await _controller.future;
unawaited(controller._updatePolylines(
PolylineUpdates.from(_polylines.values.toSet(), widget.polylines)));
_polylines = keyByPolylineId(widget.polylines);
}
Future<void> _updateCircles() async {
final GoogleMapController controller = await _controller.future;
unawaited(controller._updateCircles(
CircleUpdates.from(_circles.values.toSet(), widget.circles)));
_circles = keyByCircleId(widget.circles);
}
Future<void> _updateTileOverlays() async {
final GoogleMapController controller = await _controller.future;
unawaited(controller._updateTileOverlays(widget.tileOverlays));
}
Future<void> onPlatformViewCreated(int id) async {
final GoogleMapController controller = await GoogleMapController.init(
id,
widget.initialCameraPosition,
this,
);
_controller.complete(controller);
unawaited(_updateTileOverlays());
final MapCreatedCallback? onMapCreated = widget.onMapCreated;
if (onMapCreated != null) {
onMapCreated(controller);
}
}
void onMarkerTap(MarkerId markerId) {
final Marker? marker = _markers[markerId];
if (marker == null) {
throw UnknownMapObjectIdError('marker', markerId, 'onTap');
}
final VoidCallback? onTap = marker.onTap;
if (onTap != null) {
onTap();
}
}
void onMarkerDragStart(MarkerId markerId, LatLng position) {
final Marker? marker = _markers[markerId];
if (marker == null) {
throw UnknownMapObjectIdError('marker', markerId, 'onDragStart');
}
final ValueChanged<LatLng>? onDragStart = marker.onDragStart;
if (onDragStart != null) {
onDragStart(position);
}
}
void onMarkerDrag(MarkerId markerId, LatLng position) {
final Marker? marker = _markers[markerId];
if (marker == null) {
throw UnknownMapObjectIdError('marker', markerId, 'onDrag');
}
final ValueChanged<LatLng>? onDrag = marker.onDrag;
if (onDrag != null) {
onDrag(position);
}
}
void onMarkerDragEnd(MarkerId markerId, LatLng position) {
final Marker? marker = _markers[markerId];
if (marker == null) {
throw UnknownMapObjectIdError('marker', markerId, 'onDragEnd');
}
final ValueChanged<LatLng>? onDragEnd = marker.onDragEnd;
if (onDragEnd != null) {
onDragEnd(position);
}
}
void onPolygonTap(PolygonId polygonId) {
final Polygon? polygon = _polygons[polygonId];
if (polygon == null) {
throw UnknownMapObjectIdError('polygon', polygonId, 'onTap');
}
final VoidCallback? onTap = polygon.onTap;
if (onTap != null) {
onTap();
}
}
void onPolylineTap(PolylineId polylineId) {
final Polyline? polyline = _polylines[polylineId];
if (polyline == null) {
throw UnknownMapObjectIdError('polyline', polylineId, 'onTap');
}
final VoidCallback? onTap = polyline.onTap;
if (onTap != null) {
onTap();
}
}
void onCircleTap(CircleId circleId) {
final Circle? circle = _circles[circleId];
if (circle == null) {
throw UnknownMapObjectIdError('marker', circleId, 'onTap');
}
final VoidCallback? onTap = circle.onTap;
if (onTap != null) {
onTap();
}
}
void onInfoWindowTap(MarkerId markerId) {
final Marker? marker = _markers[markerId];
if (marker == null) {
throw UnknownMapObjectIdError('marker', markerId, 'InfoWindow onTap');
}
final VoidCallback? onTap = marker.infoWindow.onTap;
if (onTap != null) {
onTap();
}
}
void onTap(LatLng position) {
final ArgumentCallback<LatLng>? onTap = widget.onTap;
if (onTap != null) {
onTap(position);
}
}
void onLongPress(LatLng position) {
final ArgumentCallback<LatLng>? onLongPress = widget.onLongPress;
if (onLongPress != null) {
onLongPress(position);
}
}
}
/// Builds a [MapConfiguration] from the given [map].
MapConfiguration _configurationFromMapWidget(GoogleMap map) {
assert(!map.liteModeEnabled || Platform.isAndroid);
return MapConfiguration(
webGestureHandling: map.webGestureHandling,
compassEnabled: map.compassEnabled,
mapToolbarEnabled: map.mapToolbarEnabled,
cameraTargetBounds: map.cameraTargetBounds,
mapType: map.mapType,
minMaxZoomPreference: map.minMaxZoomPreference,
rotateGesturesEnabled: map.rotateGesturesEnabled,
scrollGesturesEnabled: map.scrollGesturesEnabled,
tiltGesturesEnabled: map.tiltGesturesEnabled,
fortyFiveDegreeImageryEnabled: map.fortyFiveDegreeImageryEnabled,
trackCameraPosition: map.onCameraMove != null,
zoomControlsEnabled: map.zoomControlsEnabled,
zoomGesturesEnabled: map.zoomGesturesEnabled,
liteModeEnabled: map.liteModeEnabled,
myLocationEnabled: map.myLocationEnabled,
myLocationButtonEnabled: map.myLocationButtonEnabled,
padding: map.padding,
indoorViewEnabled: map.indoorViewEnabled,
trafficEnabled: map.trafficEnabled,
buildingsEnabled: map.buildingsEnabled,
cloudMapId: map.cloudMapId,
// A null style in the widget means no style, which is expressed as '' in
// the configuration to distinguish from no change (null).
style: map.style ?? '',
);
}