-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathapple_map.dart
444 lines (374 loc) · 14.5 KB
/
apple_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
// Copyright 2018 The Chromium 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 apple_maps_flutter;
typedef void MapCreatedCallback(AppleMapController controller);
/// Callback that receives updates to the camera position.
///
/// This callback is triggered when the platform Apple Map
/// registers a camera movement.
///
/// This is used in [AppleMap.onCameraMove].
typedef void CameraPositionCallback(CameraPosition position);
class AppleMap extends StatefulWidget {
const AppleMap({
Key key,
@required this.initialCameraPosition,
this.onMapCreated,
this.gestureRecognizers,
this.compassEnabled = true,
this.trafficEnabled = false,
this.mapType = MapType.standard,
this.minMaxZoomPreference = MinMaxZoomPreference.unbounded,
this.trackingMode = TrackingMode.none,
this.rotateGesturesEnabled = true,
this.scrollGesturesEnabled = true,
this.zoomGesturesEnabled = true,
this.pitchGesturesEnabled = true,
this.myLocationEnabled = false,
this.myLocationButtonEnabled = false,
this.padding = EdgeInsets.zero,
this.annotations,
this.polylines,
this.circles,
this.polygons,
this.onCameraMoveStarted,
this.onCameraMove,
this.onCameraIdle,
this.onTap,
this.onLongPress,
this.enableClustering = false,
}) : assert(initialCameraPosition != null),
super(key: key);
final MapCreatedCallback onMapCreated;
/// The initial position of the map's camera.
final CameraPosition initialCameraPosition;
/// True if the map should show a compass when rotated.
final bool compassEnabled;
/// True if the map should display the current traffic.
final bool trafficEnabled;
/// Type of map tiles to be rendered.
final MapType mapType;
/// The mode used to track the user location.
final TrackingMode trackingMode;
/// 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 respond to zoom gestures.
final bool zoomGesturesEnabled;
/// True if the map view should respond to tilt gestures.
final bool pitchGesturesEnabled;
/// Annotations to be placed on the map.
final Set<Annotation> annotations;
/// Polylines to be placed on the map.
final Set<Polyline> polylines;
/// Circles to be placed on the map.
final Set<Circle> circles;
/// Polygons to be placed on the map.
final Set<Polygon> polygons;
/// 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 annotation 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 [AppleMap] is tapped.
final ArgumentCallback<LatLng> onTap;
/// Called every time a [AppleMap] 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 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;
/// 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 or null, the map will only handle pointer events for gestures that
/// were not claimed by any other gesture recognizer.
final Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers;
/// The padding used on the map
///
/// The amount of additional space (measured in screen points) used for padding for the
/// native controls.
final EdgeInsets padding;
/// Enables or disables MapKit native clustering.
///
/// Warning: Experimental. This feature has only been tested with custom icon annotations.
final bool enableClustering;
@override
State createState() => _AppleMapState();
}
class _AppleMapState extends State<AppleMap> {
final Completer<AppleMapController> _controller =
Completer<AppleMapController>();
Map<AnnotationId, Annotation> _annotations = <AnnotationId, Annotation>{};
Map<PolylineId, Polyline> _polylines = <PolylineId, Polyline>{};
Map<PolygonId, Polygon> _polygons = <PolygonId, Polygon>{};
Map<CircleId, Circle> _circles = <CircleId, Circle>{};
_AppleMapOptions _appleMapOptions;
@override
Widget build(BuildContext context) {
final Map<String, dynamic> creationParams = <String, dynamic>{
'initialCameraPosition': widget.initialCameraPosition?._toMap(),
'options': _appleMapOptions.toMap(),
'annotationsToAdd': _serializeAnnotationSet(widget.annotations),
'polylinesToAdd': _serializePolylineSet(widget.polylines),
'polygonsToAdd': _serializePolygonSet(widget.polygons),
'circlesToAdd': _serializeCircleSet(widget.circles),
'clusteringEnabled': widget.enableClustering,
};
if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: 'apple_maps_plugin.luisthein.de/apple_maps',
onPlatformViewCreated: onPlatformViewCreated,
gestureRecognizers: widget.gestureRecognizers,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
return Text(
'$defaultTargetPlatform is not yet supported by the apple maps plugin');
}
@override
void initState() {
super.initState();
_appleMapOptions = _AppleMapOptions.fromWidget(widget);
_annotations = _keyByAnnotationId(widget.annotations);
_polylines = _keyByPolylineId(widget.polylines);
_polygons = _keyByPolygonId(widget.polygons);
_circles = _keyByCircleId(widget.circles);
}
@override
void didUpdateWidget(AppleMap oldWidget) {
super.didUpdateWidget(oldWidget);
_updateOptions();
_updateAnnotations();
_updatePolylines();
_updatePolygons();
_updateCircles();
}
void _updateOptions() async {
final _AppleMapOptions newOptions = _AppleMapOptions.fromWidget(widget);
final Map<String, dynamic> updates =
_appleMapOptions.updatesMap(newOptions);
if (updates.isEmpty) {
return;
}
final AppleMapController controller = await _controller.future;
controller._updateMapOptions(updates);
_appleMapOptions = newOptions;
}
void _updateAnnotations() async {
final AppleMapController controller = await _controller.future;
controller._updateAnnotations(_AnnotationUpdates.from(
_annotations.values.toSet(), widget.annotations));
_annotations = _keyByAnnotationId(widget.annotations);
_annotations.forEach((key, value) {
print('Id: ${key.value} icon = ${value.icon._toJson()}');
});
}
void _updatePolylines() async {
final AppleMapController controller = await _controller.future;
controller._updatePolylines(
_PolylineUpdates.from(_polylines.values.toSet(), widget.polylines));
_polylines = _keyByPolylineId(widget.polylines);
}
void _updatePolygons() async {
final AppleMapController controller = await _controller.future;
// ignore: unawaited_futures
controller._updatePolygons(
_PolygonUpdates.from(_polygons.values.toSet(), widget.polygons));
_polygons = _keyByPolygonId(widget.polygons);
}
void _updateCircles() async {
final AppleMapController controller = await _controller.future;
// ignore: unawaited_futures
controller._updateCircles(
_CircleUpdates.from(_circles.values.toSet(), widget.circles));
_circles = _keyByCircleId(widget.circles);
}
Future<void> onPlatformViewCreated(int id) async {
final AppleMapController controller = await AppleMapController.init(
id,
widget.initialCameraPosition,
this,
);
_controller.complete(controller);
if (widget.onMapCreated != null) {
widget.onMapCreated(controller);
}
}
void onAnnotationTap(String annotationIdParam) {
assert(annotationIdParam != null);
final AnnotationId annotationId = AnnotationId(annotationIdParam);
if (_annotations[annotationId]?.onTap != null) {
_annotations[annotationId].onTap();
}
}
void onAnnotationDragEnd(String annotationIdParam, LatLng position) {
assert(annotationIdParam != null);
final AnnotationId annotationId = AnnotationId(annotationIdParam);
if (_annotations[annotationId]?.onDragEnd != null) {
_annotations[annotationId].onDragEnd(position);
}
}
void onPolylineTap(String polylineIdParam) {
assert(polylineIdParam != null);
final PolylineId polylineId = PolylineId(polylineIdParam);
if (_polylines[polylineId]?.onTap != null) {
_polylines[polylineId].onTap();
}
}
void onPolygonTap(String polygonIdParam) {
assert(polygonIdParam != null);
final PolygonId polygonId = PolygonId(polygonIdParam);
_polygons[polygonId].onTap();
}
void onCircleTap(String circleIdParam) {
assert(circleIdParam != null);
final CircleId circleId = CircleId(circleIdParam);
_circles[circleId].onTap();
}
void onInfoWindowTap(String annotationIdParam) {
assert(annotationIdParam != null);
final AnnotationId annotationId = AnnotationId(annotationIdParam);
if (_annotations[annotationId]?.infoWindow?.onTap != null) {
_annotations[annotationId].infoWindow.onTap();
}
}
void onTap(LatLng position) {
assert(position != null);
if (widget.onTap != null) {
widget.onTap(position);
}
}
void onLongPress(LatLng position) {
assert(position != null);
if (widget.onLongPress != null) {
widget.onLongPress(position);
}
}
}
/// Configuration options for the AppleMaps user interface.
///
/// When used to change configuration, null values will be interpreted as
/// "do not change this configuration option".
class _AppleMapOptions {
_AppleMapOptions({
this.compassEnabled,
this.trafficEnabled,
this.mapType,
this.minMaxZoomPreference,
this.rotateGesturesEnabled,
this.scrollGesturesEnabled,
this.pitchGesturesEnabled,
this.trackingMode,
this.zoomGesturesEnabled,
this.myLocationEnabled,
this.myLocationButtonEnabled,
this.padding,
});
static _AppleMapOptions fromWidget(AppleMap map) {
return _AppleMapOptions(
compassEnabled: map.compassEnabled,
trafficEnabled: map.trafficEnabled,
mapType: map.mapType,
minMaxZoomPreference: map.minMaxZoomPreference,
rotateGesturesEnabled: map.rotateGesturesEnabled,
scrollGesturesEnabled: map.scrollGesturesEnabled,
pitchGesturesEnabled: map.pitchGesturesEnabled,
trackingMode: map.trackingMode,
zoomGesturesEnabled: map.zoomGesturesEnabled,
myLocationEnabled: map.myLocationEnabled,
myLocationButtonEnabled: map.myLocationButtonEnabled,
padding: map.padding,
);
}
final bool compassEnabled;
final bool trafficEnabled;
final MapType mapType;
final MinMaxZoomPreference minMaxZoomPreference;
final bool rotateGesturesEnabled;
final bool scrollGesturesEnabled;
final bool pitchGesturesEnabled;
final TrackingMode trackingMode;
final bool zoomGesturesEnabled;
final bool myLocationEnabled;
final bool myLocationButtonEnabled;
final EdgeInsets padding;
Map<String, dynamic> toMap() {
final Map<String, dynamic> optionsMap = <String, dynamic>{};
void addIfNonNull(String fieldName, dynamic value) {
if (value != null) {
optionsMap[fieldName] = value;
}
}
addIfNonNull('compassEnabled', compassEnabled);
addIfNonNull('trafficEnabled', trafficEnabled);
addIfNonNull('mapType', mapType?.index);
addIfNonNull('minMaxZoomPreference', minMaxZoomPreference?._toJson());
addIfNonNull('rotateGesturesEnabled', rotateGesturesEnabled);
addIfNonNull('scrollGesturesEnabled', scrollGesturesEnabled);
addIfNonNull('pitchGesturesEnabled', pitchGesturesEnabled);
addIfNonNull('zoomGesturesEnabled', zoomGesturesEnabled);
addIfNonNull('trackingMode', trackingMode?.index);
addIfNonNull('myLocationEnabled', myLocationEnabled);
addIfNonNull('myLocationButtonEnabled', myLocationButtonEnabled);
addIfNonNull('padding', _serializePadding(padding));
return optionsMap;
}
Map<String, dynamic> updatesMap(_AppleMapOptions newOptions) {
final Map<String, dynamic> prevOptionsMap = toMap();
return newOptions.toMap()
..removeWhere(
(String key, dynamic value) => prevOptionsMap[key] == value);
}
List<double> _serializePadding(EdgeInsets insets) {
if (insets == null) return null;
return [insets.top, insets.left, insets.bottom, insets.right];
}
}