-
-
Notifications
You must be signed in to change notification settings - Fork 866
/
marker_layer.dart
162 lines (139 loc) · 4.27 KB
/
marker_layer.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
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/bounds.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong/latlong.dart';
class MarkerLayerOptions extends LayerOptions {
final List<Marker> markers;
MarkerLayerOptions({
Key key,
this.markers = const [],
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild);
}
class Anchor {
final double left;
final double top;
Anchor(this.left, this.top);
Anchor._(double width, double height, AnchorAlign alignOpt)
: left = _leftOffset(width, alignOpt),
top = _topOffset(height, alignOpt);
static double _leftOffset(double width, AnchorAlign alignOpt) {
switch (alignOpt) {
case AnchorAlign.left:
return 0.0;
case AnchorAlign.right:
return width;
case AnchorAlign.top:
case AnchorAlign.bottom:
case AnchorAlign.center:
default:
return width / 2;
}
}
static double _topOffset(double height, AnchorAlign alignOpt) {
switch (alignOpt) {
case AnchorAlign.top:
return 0.0;
case AnchorAlign.bottom:
return height;
case AnchorAlign.left:
case AnchorAlign.right:
case AnchorAlign.center:
default:
return height / 2;
}
}
factory Anchor.forPos(AnchorPos pos, double width, double height) {
if (pos == null) return Anchor._(width, height, null);
if (pos.value is AnchorAlign) return Anchor._(width, height, pos.value);
if (pos.value is Anchor) return pos.value;
throw Exception('Unsupported AnchorPos value type: ${pos.runtimeType}.');
}
}
class AnchorPos<T> {
AnchorPos._(this.value);
T value;
static AnchorPos exactly(Anchor anchor) => AnchorPos._(anchor);
static AnchorPos align(AnchorAlign alignOpt) => AnchorPos._(alignOpt);
}
enum AnchorAlign {
left,
right,
top,
bottom,
center,
}
class Marker {
final LatLng point;
final WidgetBuilder builder;
final double width;
final double height;
final Anchor anchor;
Marker({
this.point,
this.builder,
this.width = 30.0,
this.height = 30.0,
AnchorPos anchorPos,
}) : anchor = Anchor.forPos(anchorPos, width, height);
}
class MarkerLayerWidget extends StatelessWidget {
final MarkerLayerOptions options;
MarkerLayerWidget({Key key, @required this.options}) : super(key: key);
@override
Widget build(BuildContext context) {
final mapState = MapState.of(context);
return MarkerLayer(options, mapState, mapState.onMoved);
}
}
class MarkerLayer extends StatelessWidget {
final MarkerLayerOptions markerOpts;
final MapState map;
final Stream<Null> stream;
MarkerLayer(this.markerOpts, this.map, this.stream)
: super(key: markerOpts.key);
bool _boundsContainsMarker(Marker marker) {
var pixelPoint = map.project(marker.point);
final width = marker.width - marker.anchor.left;
final height = marker.height - marker.anchor.top;
var sw = CustomPoint(pixelPoint.x + width, pixelPoint.y - height);
var ne = CustomPoint(pixelPoint.x - width, pixelPoint.y + height);
return map.pixelBounds.containsPartialBounds(Bounds(sw, ne));
}
@override
Widget build(BuildContext context) {
return StreamBuilder<int>(
stream: stream, // a Stream<int> or null
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
var markers = <Widget>[];
for (var markerOpt in markerOpts.markers) {
var pos = map.project(markerOpt.point);
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();
var pixelPosX =
(pos.x - (markerOpt.width - markerOpt.anchor.left)).toDouble();
var pixelPosY =
(pos.y - (markerOpt.height - markerOpt.anchor.top)).toDouble();
if (!_boundsContainsMarker(markerOpt)) {
continue;
}
markers.add(
Positioned(
width: markerOpt.width,
height: markerOpt.height,
left: pixelPosX,
top: pixelPosY,
child: markerOpt.builder(context),
),
);
}
return Container(
child: Stack(
children: markers,
),
);
},
);
}
}