-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathannotation.dart
300 lines (258 loc) · 8.89 KB
/
annotation.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
// 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;
dynamic _offsetToJson(Offset? offset) {
if (offset == null) {
return null;
}
return <dynamic>[offset.dx, offset.dy];
}
/// Text labels for a [Annotation] info window.
class InfoWindow {
const InfoWindow({
this.title,
this.snippet,
this.anchor = const Offset(0.5, 0.0),
this.onTap,
});
/// Text labels specifying that no text is to be displayed.
static const InfoWindow noText = InfoWindow();
/// Text displayed in an info window when the user taps the annotation.
///
/// A null value means no title.
final String? title;
/// Additional text displayed below the [title].
///
/// A null value means no additional text.
final String? snippet;
/// The icon image point that will be the anchor of the info window when
/// displayed.
///
/// The image point is specified in normalized coordinates: An anchor of
/// (0.0, 0.0) means the top left corner of the image. An anchor
/// of (1.0, 1.0) means the bottom right corner of the image.
final Offset anchor;
/// onTap callback for this [InfoWindow].
final VoidCallback? onTap;
/// Creates a new [InfoWindow] object whose values are the same as this instance,
/// unless overwritten by the specified parameters.
InfoWindow copyWith({
String? titleParam,
String? snippetParam,
Offset? anchorParam,
VoidCallback? onTapParam,
}) {
return InfoWindow(
title: titleParam ?? title,
snippet: snippetParam ?? snippet,
anchor: anchorParam ?? anchor,
onTap: onTapParam ?? onTap,
);
}
dynamic _toJson() {
final Map<String, dynamic> json = <String, dynamic>{};
void addIfPresent(String fieldName, dynamic value) {
if (value != null) {
json[fieldName] = value;
}
}
addIfPresent('title', title);
addIfPresent('snippet', snippet);
addIfPresent('anchor', _offsetToJson(anchor));
addIfPresent('consumesTapEvents', onTap != null);
return json;
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! InfoWindow) return false;
final InfoWindow typedOther = other;
return title == typedOther.title &&
snippet == typedOther.snippet &&
anchor == typedOther.anchor &&
onTap == typedOther.onTap;
}
@override
int get hashCode => Object.hash(title.hashCode, snippet, anchor);
@override
String toString() {
return 'InfoWindow{title: $title, snippet: $snippet, anchor: $anchor, consumesTapEvents: ${onTap != null}}';
}
}
/// Uniquely identifies a [Annotation] among [AppleMap] annotations.
///
/// This does not have to be globally unique, only unique among the list.
@immutable
class AnnotationId {
AnnotationId(this.value);
/// value of the [AnnotationId].
final String value;
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! AnnotationId) return false;
final AnnotationId typedOther = other;
return value == typedOther.value;
}
@override
int get hashCode => value.hashCode;
@override
String toString() {
return 'AnnotationId{value: $value}';
}
}
/// Marks a geographical location on the map.
///
/// A annotation icon is drawn oriented against the device's screen rather than
/// the map's surface; that is, it will not necessarily change orientation
/// due to map rotations, tilting, or zooming.
@immutable
class Annotation {
/// Creates a set of annotation configuration options.
///
/// Default annotation options.
///
/// Specifies a annotation that
/// * is fully opaque; [alpha] is 1.0
/// * has default tap handling; [consumeTapEvents] is false
/// * is stationary; [draggable] is false
/// * has a default icon; [icon] is default Pin Annotation
/// * has no info window text; [infoWindowText] is `InfoWindowText.noText`
/// * is positioned at 0, 0; [position] is `LatLng(0.0, 0.0)`
/// * is visible; [visible] is true
Annotation({
required this.annotationId,
this.alpha = 1.0,
this.anchor = const Offset(0.5, 1.0),
this.draggable = false,
this.icon = BitmapDescriptor.defaultAnnotation,
this.infoWindow = InfoWindow.noText,
this.position = const LatLng(0.0, 0.0),
this.onTap,
this.visible = true,
this.zIndex = -1,
this.onDragEnd,
}) : assert(0.0 <= alpha && alpha <= 1.0);
/// Uniquely identifies a [Annotation].
final AnnotationId annotationId;
/// The opacity of the annotation, between 0.0 and 1.0 inclusive.
///
/// 0.0 means fully transparent, 1.0 means fully opaque.
final double alpha;
/// The icon image point that will be placed at the [position] of the marker.
///
/// The image point is specified in normalized coordinates: An anchor of
/// (0.0, 0.0) means the top left corner of the image. An anchor
/// of (1.0, 1.0) means the bottom right corner of the image.
final Offset anchor;
/// True if the annotation is draggable by user touch events.
final bool draggable;
/// A description of the bitmap used to draw the annotation icon.
final BitmapDescriptor icon;
/// An Apple Maps InfoWindow.
///
/// The window is displayed when the annotation is tapped.
final InfoWindow infoWindow;
/// Geographical location of the annotation.
final LatLng position;
/// Callbacks to receive tap events for annotations placed on this map.
final VoidCallback? onTap;
/// True if the annotation is visible.
final bool visible;
final ValueChanged<LatLng>? onDragEnd;
/// The z-index of the annotation, used to determine relative drawing order of
/// map overlays.
///
/// Overlays are drawn in order of z-index, so that lower values means drawn
/// earlier, and thus appearing to be closer to the surface of the Earth.
double zIndex;
/// Creates a new [Annotation] object whose values are the same as this instance,
/// unless overwritten by the specified parameters.
Annotation copyWith({
double? alphaParam,
Offset? anchorParam,
bool? consumeTapEventsParam,
bool? draggableParam,
BitmapDescriptor? iconParam,
InfoWindow? infoWindowParam,
LatLng? positionParam,
bool? visibleParam,
double? zIndexParam,
VoidCallback? onTapParam,
ValueChanged<LatLng>? onDragEndParam,
}) {
return Annotation(
annotationId: annotationId,
anchor: anchorParam ?? anchor,
alpha: alphaParam ?? alpha,
draggable: draggableParam ?? draggable,
icon: iconParam ?? icon,
infoWindow: infoWindowParam ?? infoWindow,
position: positionParam ?? position,
onTap: onTapParam ?? onTap,
visible: visibleParam ?? visible,
zIndex: zIndexParam ?? zIndex,
onDragEnd: onDragEndParam ?? onDragEnd,
);
}
Map<String, dynamic> _toJson() {
final Map<String, dynamic> json = <String, dynamic>{};
void addIfPresent(String fieldName, dynamic value) {
if (value != null) {
json[fieldName] = value;
}
}
addIfPresent('annotationId', annotationId.value);
addIfPresent('alpha', alpha);
addIfPresent('anchor', _offsetToJson(anchor));
addIfPresent('draggable', draggable);
addIfPresent('icon', icon._toJson());
addIfPresent('infoWindow', infoWindow._toJson());
addIfPresent('visible', visible);
addIfPresent('position', position._toJson());
addIfPresent('zIndex', zIndex);
return json;
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! Annotation) return false;
final Annotation typedOther = other;
return annotationId == typedOther.annotationId &&
alpha == typedOther.alpha &&
anchor == typedOther.anchor &&
draggable == typedOther.draggable &&
icon == typedOther.icon &&
infoWindow == typedOther.infoWindow &&
position == typedOther.position &&
visible == typedOther.visible &&
zIndex == typedOther.zIndex;
}
@override
int get hashCode => annotationId.hashCode;
@override
String toString() {
return 'Annotation{annotationId: $annotationId, alpha: $alpha, draggable: $draggable, '
'icon: $icon, infoWindow: $infoWindow, position: $position ,visible: $visible, '
'onTap: $onTap}, zIndex: $zIndex, onTap: $onTap}';
}
}
Map<AnnotationId, Annotation> _keyByAnnotationId(
Iterable<Annotation>? annotations) {
if (annotations == null) {
return <AnnotationId, Annotation>{};
}
return Map<AnnotationId, Annotation>.fromEntries(annotations.map(
(Annotation annotation) => MapEntry<AnnotationId, Annotation>(
annotation.annotationId, annotation)));
}
List<Map<String, dynamic>>? _serializeAnnotationSet(
Set<Annotation>? annotations) {
if (annotations == null) {
return null;
}
return annotations
.map<Map<String, dynamic>>((Annotation m) => m._toJson())
.toList();
}