-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmarker.dart
287 lines (252 loc) · 9.28 KB
/
marker.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
part of platform_maps_flutter;
/// Text labels for a [Marker] info window.
class InfoWindow {
const InfoWindow({this.title, this.snippet, this.onTap, this.anchor});
/// 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 marker.
///
/// 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;
appleMaps.InfoWindow get appleMapsInfoWindow => appleMaps.InfoWindow(
anchor: this.anchor ?? Offset(0, 0),
onTap: this.onTap,
snippet: this.snippet,
title: this.title,
);
googleMaps.InfoWindow get googleMapsInfoWindow => googleMaps.InfoWindow(
anchor: this.anchor ?? Offset(0, 0),
onTap: this.onTap,
snippet: this.snippet,
title: this.title,
);
/// 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,
);
}
}
/// Uniquely identifies a [Marker] among [PlatformMaps] markers.
///
/// This does not have to be globally unique, only unique among the list.
@immutable
class MarkerId {
MarkerId(this.value);
/// value of the [MarkerId].
final String value;
appleMaps.AnnotationId get appleMapsAnnoationId => appleMaps.AnnotationId(
this.value,
);
googleMaps.MarkerId get googleMapsMarkerId => googleMaps.MarkerId(
this.value,
);
}
/// Marks a geographical location on the map.
///
/// A marker 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 Marker {
/// Creates a set of marker configuration options.
///
/// Default marker options.
///
/// Specifies a marker 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 `BitmapDescriptor.defaultMarker`
/// * 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
/// * is placed at the base of the drawing order; [zIndex] is 0.0
const Marker({
required this.markerId,
this.alpha = 1.0,
this.anchor = const Offset(0.5, 1.0),
this.consumeTapEvents = false,
this.draggable = false,
this.icon,
this.infoWindow = InfoWindow.noText,
this.position = const LatLng(0.0, 0.0),
this.onTap,
this.visible = true,
this.onDragEnd,
this.zIndex,
}) : assert((0.0 <= alpha && alpha <= 1.0));
/// Uniquely identifies a [Marker].
final MarkerId markerId;
/// The opacity of the marker, 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 marker icon consumes tap events. If not, the map will perform
/// default tap handling by centering the map on the marker and displaying its
/// info window.
final bool consumeTapEvents;
/// True if the marker is draggable by user touch events.
final bool draggable;
/// A description of the bitmap used to draw the marker icon.
final BitmapDescriptor? icon;
/// A Google Maps InfoWindow.
///
/// The window is displayed when the marker is tapped.
final InfoWindow infoWindow;
/// Geographical location of the marker.
final LatLng position;
/// Callbacks to receive tap events for markers placed on this map.
final VoidCallback? onTap;
/// True if the annotation is visible.
final bool visible;
/// zIndex value for the marker.
final double? zIndex;
final ValueChanged<LatLng>? onDragEnd;
appleMaps.Annotation get appleMapsAnnotation => appleMaps.Annotation(
annotationId: this.markerId.appleMapsAnnoationId,
alpha: this.alpha,
anchor: this.anchor,
draggable: this.draggable,
infoWindow: this.infoWindow.appleMapsInfoWindow,
onTap: this.onTap,
icon: this.icon?.bitmapDescriptor ??
BitmapDescriptor.defaultMarker?.bitmapDescriptor,
visible: this.visible,
onDragEnd: this.onDragEnd != null
? (appleMaps.LatLng latLng) =>
_onAppleAnnotationDragEnd(latLng, this.onDragEnd)
: null,
position: this.position.appleLatLng,
zIndex: this.zIndex ?? -1,
);
googleMaps.Marker get googleMapsMarker => googleMaps.Marker(
markerId: this.markerId.googleMapsMarkerId,
alpha: this.alpha,
anchor: this.anchor,
draggable: this.draggable,
infoWindow: this.infoWindow.googleMapsInfoWindow,
onTap: this.onTap,
icon: this.icon?.bitmapDescriptor ??
BitmapDescriptor.defaultMarker?.bitmapDescriptor,
visible: this.visible,
onDragEnd: this.onDragEnd != null
? (googleMaps.LatLng latLng) =>
_onGoogleMarkerDragEnd(latLng, this.onDragEnd)
: null,
position: this.position.googleLatLng,
zIndex: this.zIndex ?? 0.0,
);
static appleMaps.Annotation appleMapsAnnotationFromMarker(Marker marker) =>
appleMaps.Annotation(
annotationId: marker.markerId.appleMapsAnnoationId,
alpha: marker.alpha,
anchor: marker.anchor,
draggable: marker.draggable,
infoWindow: marker.infoWindow.appleMapsInfoWindow,
onTap: marker.onTap,
icon: marker.icon?.bitmapDescriptor ??
BitmapDescriptor.defaultMarker?.bitmapDescriptor,
visible: marker.visible,
onDragEnd: marker.onDragEnd != null
? (appleMaps.LatLng latLng) =>
_onAppleAnnotationDragEnd(latLng, marker.onDragEnd)
: null,
position: marker.position.appleLatLng,
zIndex: marker.zIndex ?? -1,
);
static googleMaps.Marker googleMapsMarkerFromMarker(Marker marker) =>
googleMaps.Marker(
markerId: marker.markerId.googleMapsMarkerId,
alpha: marker.alpha,
anchor: marker.anchor,
draggable: marker.draggable,
infoWindow: marker.infoWindow.googleMapsInfoWindow,
onTap: marker.onTap,
icon: marker.icon?.bitmapDescriptor ??
BitmapDescriptor.defaultMarker?.bitmapDescriptor,
visible: marker.visible,
onDragEnd: marker.onDragEnd != null
? (googleMaps.LatLng latLng) =>
_onGoogleMarkerDragEnd(latLng, marker.onDragEnd)
: null,
position: marker.position.googleLatLng,
zIndex: marker.zIndex ?? 0.0,
);
static Set<appleMaps.Annotation> toAppleMapsAnnotationSet(
Set<Marker> markers) {
List<appleMaps.Annotation> _annotations = <appleMaps.Annotation>[];
for (Marker marker in markers) {
_annotations.add(appleMapsAnnotationFromMarker(marker));
}
return Set.from(_annotations);
}
static Set<googleMaps.Marker> toGoogleMapsMarkerSet(Set<Marker> markers) {
List<googleMaps.Marker> _markers = <googleMaps.Marker>[];
for (Marker marker in markers) {
_markers.add(googleMapsMarkerFromMarker(marker));
}
return Set.from(_markers);
}
Marker copyWith({
double? alphaParam,
Offset? anchorParam,
bool? consumeTapEventsParam,
bool? draggableParam,
BitmapDescriptor? iconParam,
InfoWindow? infoWindowParam,
LatLng? positionParam,
bool? visibleParam,
VoidCallback? onTapParam,
double? zIndexParam,
}) {
return Marker(
markerId: markerId,
alpha: alphaParam ?? alpha,
anchor: anchorParam ?? anchor,
consumeTapEvents: consumeTapEventsParam ?? consumeTapEvents,
draggable: draggableParam ?? draggable,
icon: iconParam ?? icon,
infoWindow: infoWindowParam ?? infoWindow,
position: positionParam ?? position,
visible: visibleParam ?? visible,
onTap: onTapParam ?? onTap,
zIndex: zIndexParam ?? zIndex,
);
}
static _onGoogleMarkerDragEnd(googleMaps.LatLng latLng, Function? onDragEnd) {
onDragEnd?.call(LatLng._fromGoogleLatLng(latLng));
}
static _onAppleAnnotationDragEnd(
appleMaps.LatLng latLng, Function? onDragEnd) {
onDragEnd?.call(LatLng._fromAppleLatLng(latLng));
}
}