forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_maps_flutter_web.dart
337 lines (291 loc) · 9.7 KB
/
google_maps_flutter_web.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
// 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_web;
/// The web implementation of [GoogleMapsFlutterPlatform].
///
/// This class implements the `package:google_maps_flutter` functionality for the web.
class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
/// Registers this class as the default instance of [GoogleMapsFlutterPlatform].
static void registerWith(Registrar registrar) {
GoogleMapsFlutterPlatform.instance = GoogleMapsPlugin();
}
// A cache of map controllers by map Id.
Map _mapById = Map<int, GoogleMapController>();
/// Allows tests to inject controllers without going through the buildView flow.
@visibleForTesting
void debugSetMapById(Map<int, GoogleMapController> mapById) {
_mapById = mapById;
}
// Convenience getter for a stream of events filtered by their mapId.
Stream<MapEvent> _events(int mapId) => _map(mapId).events;
// Convenience getter for a map controller by its mapId.
GoogleMapController _map(int mapId) {
final controller = _mapById[mapId];
assert(controller != null,
'Maps cannot be retrieved before calling buildView!');
return controller;
}
@override
Future<void> init(int mapId) async {
// The internal instance of our controller is initialized eagerly in `buildView`,
// so we don't have to do anything in this method, which is left intentionally
// blank.
assert(_map(mapId) != null, 'Must call buildWidget before init!');
}
/// Updates the options of a given `mapId`.
///
/// This attempts to merge the new `optionsUpdate` passed in, with the previous
/// options passed to the map (in other updates, or when creating it).
@override
Future<void> updateMapOptions(
Map<String, dynamic> optionsUpdate, {
required int mapId,
}) async {
_map(mapId).updateRawOptions(optionsUpdate);
}
/// Applies the passed in `markerUpdates` to the `mapId`.
@override
Future<void> updateMarkers(
MarkerUpdates markerUpdates, {
required int mapId,
}) async {
_map(mapId).updateMarkers(markerUpdates);
}
/// Applies the passed in `polygonUpdates` to the `mapId`.
@override
Future<void> updatePolygons(
PolygonUpdates polygonUpdates, {
required int mapId,
}) async {
_map(mapId).updatePolygons(polygonUpdates);
}
/// Applies the passed in `polylineUpdates` to the `mapId`.
@override
Future<void> updatePolylines(
PolylineUpdates polylineUpdates, {
required int mapId,
}) async {
_map(mapId).updatePolylines(polylineUpdates);
}
/// Applies the passed in `circleUpdates` to the `mapId`.
@override
Future<void> updateCircles(
CircleUpdates circleUpdates, {
required int mapId,
}) async {
_map(mapId).updateCircles(circleUpdates);
}
@override
Future<void> updateTileOverlays({
required Set<TileOverlay> newTileOverlays,
required int mapId,
}) async {
return; // Noop for now!
}
@override
Future<void> clearTileCache(
TileOverlayId tileOverlayId, {
required int mapId,
}) async {
return; // Noop for now!
}
/// Applies the given `cameraUpdate` to the current viewport (with animation).
@override
Future<void> animateCamera(
CameraUpdate cameraUpdate, {
required int mapId,
}) async {
return moveCamera(cameraUpdate, mapId: mapId);
}
/// Applies the given `cameraUpdate` to the current viewport.
@override
Future<void> moveCamera(
CameraUpdate cameraUpdate, {
required int mapId,
}) async {
return _map(mapId).moveCamera(cameraUpdate);
}
/// Sets the passed-in `mapStyle` to the map.
///
/// This function just adds a 'styles' option to the current map options.
///
/// Subsequent calls to this method override previous calls, you need to
/// pass full styles.
@override
Future<void> setMapStyle(
String? mapStyle, {
required int mapId,
}) async {
_map(mapId).updateRawOptions({
'styles': _mapStyles(mapStyle),
});
}
/// Returns the bounds of the current viewport.
@override
Future<LatLngBounds> getVisibleRegion({
required int mapId,
}) {
return _map(mapId).getVisibleRegion();
}
/// Returns the screen coordinate (in pixels) of a given `latLng`.
@override
Future<ScreenCoordinate> getScreenCoordinate(
LatLng latLng, {
required int mapId,
}) {
return _map(mapId).getScreenCoordinate(latLng);
}
/// Returns the [LatLng] of a [ScreenCoordinate] of the viewport.
@override
Future<LatLng> getLatLng(
ScreenCoordinate screenCoordinate, {
required int mapId,
}) {
return _map(mapId).getLatLng(screenCoordinate);
}
/// Shows the [InfoWindow] (if any) of the [Marker] identified by `markerId`.
///
/// See also:
/// * [hideMarkerInfoWindow] to hide the info window.
/// * [isMarkerInfoWindowShown] to check if the info window is visible/hidden.
@override
Future<void> showMarkerInfoWindow(
MarkerId markerId, {
required int mapId,
}) async {
_map(mapId).showInfoWindow(markerId);
}
/// Hides the [InfoWindow] (if any) of the [Marker] identified by `markerId`.
///
/// See also:
/// * [showMarkerInfoWindow] to show the info window.
/// * [isMarkerInfoWindowShown] to check if the info window is shown.
@override
Future<void> hideMarkerInfoWindow(
MarkerId markerId, {
required int mapId,
}) async {
_map(mapId).hideInfoWindow(markerId);
}
/// Returns true if the [InfoWindow] of the [Marker] identified by `markerId` is shown.
///
/// See also:
/// * [showMarkerInfoWindow] to show the info window.
/// * [hideMarkerInfoWindow] to hide the info window.
@override
Future<bool> isMarkerInfoWindowShown(
MarkerId markerId, {
required int mapId,
}) async {
return _map(mapId).isInfoWindowShown(markerId);
}
/// Returns the zoom level of the `mapId`.
@override
Future<double> getZoomLevel({
required int mapId,
}) {
return _map(mapId).getZoomLevel();
}
// The following are the 11 possible streams of data from the native side
// into the plugin
@override
Stream<CameraMoveStartedEvent> onCameraMoveStarted({required int mapId}) {
return _events(mapId).whereType<CameraMoveStartedEvent>();
}
@override
Stream<CameraMoveEvent> onCameraMove({required int mapId}) {
return _events(mapId).whereType<CameraMoveEvent>();
}
@override
Stream<CameraIdleEvent> onCameraIdle({required int mapId}) {
return _events(mapId).whereType<CameraIdleEvent>();
}
@override
Stream<MarkerTapEvent> onMarkerTap({required int mapId}) {
return _events(mapId).whereType<MarkerTapEvent>();
}
@override
Stream<InfoWindowTapEvent> onInfoWindowTap({required int mapId}) {
return _events(mapId).whereType<InfoWindowTapEvent>();
}
@override
Stream<MarkerDragStartEvent> onMarkerDragStart({required int mapId}) {
return _events(mapId).whereType<MarkerDragStartEvent>();
}
@override
Stream<MarkerDragEvent> onMarkerDrag({required int mapId}) {
return _events(mapId).whereType<MarkerDragEvent>();
}
@override
Stream<MarkerDragEndEvent> onMarkerDragEnd({required int mapId}) {
return _events(mapId).whereType<MarkerDragEndEvent>();
}
@override
Stream<PolylineTapEvent> onPolylineTap({required int mapId}) {
return _events(mapId).whereType<PolylineTapEvent>();
}
@override
Stream<PolygonTapEvent> onPolygonTap({required int mapId}) {
return _events(mapId).whereType<PolygonTapEvent>();
}
@override
Stream<CircleTapEvent> onCircleTap({required int mapId}) {
return _events(mapId).whereType<CircleTapEvent>();
}
@override
Stream<MapTapEvent> onTap({required int mapId}) {
return _events(mapId).whereType<MapTapEvent>();
}
@override
Stream<MapLongPressEvent> onLongPress({required int mapId}) {
return _events(mapId).whereType<MapLongPressEvent>();
}
/// Disposes of the current map. It can't be used afterwards!
@override
void dispose({required int mapId}) {
_map(mapId).dispose();
_mapById.remove(mapId);
}
@override
Widget buildView(
int creationId,
PlatformViewCreatedCallback onPlatformViewCreated, {
required CameraPosition initialCameraPosition,
Set<Marker> markers = const <Marker>{},
Set<Polygon> polygons = const <Polygon>{},
Set<Polyline> polylines = const <Polyline>{},
Set<Circle> circles = const <Circle>{},
Set<TileOverlay> tileOverlays = const <TileOverlay>{},
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers =
const <Factory<OneSequenceGestureRecognizer>>{},
Map<String, dynamic> mapOptions = const <String, dynamic>{},
}) {
// Bail fast if we've already rendered this map ID...
if (_mapById[creationId]?.widget != null) {
return _mapById[creationId].widget;
}
final StreamController<MapEvent> controller =
StreamController<MapEvent>.broadcast();
final mapController = GoogleMapController(
initialCameraPosition: initialCameraPosition,
mapId: creationId,
streamController: controller,
markers: markers,
polygons: polygons,
polylines: polylines,
circles: circles,
mapOptions: mapOptions,
)..init(); // Initialize the controller
_mapById[creationId] = mapController;
mapController.events.whereType<WebMapReadyEvent>().first.then((event) {
assert(creationId == event.mapId,
'Received WebMapReadyEvent for the wrong map');
// Notify the plugin now that there's a fully initialized controller.
onPlatformViewCreated.call(event.mapId);
});
assert(mapController.widget != null,
'The widget of a GoogleMapController cannot be null before calling dispose on it.');
return mapController.widget!;
}
}