forked from flutter-mapbox-gl/maps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.dart
More file actions
288 lines (264 loc) · 7.09 KB
/
layer.dart
File metadata and controls
288 lines (264 loc) · 7.09 KB
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
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:mapbox_gl/mapbox_gl.dart';
import 'package:mapbox_gl_example/main.dart';
import 'package:mapbox_gl_example/page.dart';
class LayerPage extends ExamplePage {
LayerPage() : super(const Icon(Icons.share), 'Layer');
@override
Widget build(BuildContext context) => LayerBody();
}
class LayerBody extends StatefulWidget {
@override
State<StatefulWidget> createState() => LayerState();
}
class LayerState extends State {
static final LatLng center = const LatLng(-33.86711, 151.1947171);
late MapboxMapController controller;
Timer? bikeTimer;
Timer? filterTimer;
int filteredId = 0;
@override
Widget build(BuildContext context) {
return MapboxMap(
accessToken: MapsDemo.ACCESS_TOKEN,
dragEnabled: false,
myLocationEnabled: true,
onMapCreated: _onMapCreated,
onMapClick: (point, latLong) =>
print(point.toString() + latLong.toString()),
onStyleLoadedCallback: _onStyleLoadedCallback,
initialCameraPosition: CameraPosition(
target: center,
zoom: 11.0,
),
annotationOrder: const [],
);
}
void _onMapCreated(MapboxMapController controller) {
this.controller = controller;
controller.onFeatureTapped.add(onFeatureTap);
}
void onFeatureTap(dynamic featureId, Point<double> point, LatLng latLng) {
final snackBar = SnackBar(
content: Text(
'Tapped feature with id $featureId',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
backgroundColor: Theme.of(context).primaryColor,
);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
void _onStyleLoadedCallback() async {
await controller.addGeoJsonSource("points", _points);
await controller.addGeoJsonSource("moving", _movingFeature(0));
//new style of adding sources
await controller.addSource("fills", GeojsonSourceProperties(data: _fills));
await controller.addFillLayer(
"fills",
"fills",
FillLayerProperties(fillColor: [
Expressions.interpolate,
['exponential', 0.5],
[Expressions.zoom],
11,
'red',
18,
'green'
], fillOpacity: 0.4),
belowLayerId: "water",
filter: ['==', 'id', filteredId],
);
await controller.addLineLayer(
"fills",
"lines",
LineLayerProperties(
lineColor: Colors.lightBlue.toHexStringRGB(),
lineWidth: [
Expressions.interpolate,
["linear"],
[Expressions.zoom],
11.0,
2.0,
20.0,
10.0
]),
);
await controller.addCircleLayer(
"fills",
"circles",
CircleLayerProperties(
circleRadius: 4,
circleColor: Colors.blue.toHexStringRGB(),
),
);
await controller.addSymbolLayer(
"points",
"symbols",
SymbolLayerProperties(
iconImage: "{type}-15",
iconSize: 2,
iconAllowOverlap: true,
),
);
await controller.addSymbolLayer(
"moving",
"moving",
SymbolLayerProperties(
textField: [Expressions.get, "name"],
textHaloWidth: 1,
textSize: 10,
textHaloColor: Colors.white.toHexStringRGB(),
textOffset: [
Expressions.literal,
[0, 2]
],
iconImage: "bicycle-15",
iconSize: 2,
iconAllowOverlap: true,
textAllowOverlap: true,
),
minzoom: 11,
);
bikeTimer = Timer.periodic(Duration(milliseconds: 10), (t) {
controller.setGeoJsonSource("moving", _movingFeature(t.tick / 2000));
});
filterTimer = Timer.periodic(Duration(seconds: 3), (t) {
filteredId = filteredId == 0 ? 1 : 0;
controller.setFilter('fills', ['==', 'id', filteredId]);
});
}
@override
void dispose() {
bikeTimer?.cancel();
filterTimer?.cancel();
super.dispose();
}
}
Map<String, dynamic> _movingFeature(double t) {
List<double> makeLatLong(double t) {
final angle = t * 2 * pi;
const r = 0.025;
const center_x = 151.1849;
const center_y = -33.8748;
return [
center_x + r * sin(angle),
center_y + r * cos(angle),
];
}
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "POGAČAR Tadej"},
"id": 10,
"geometry": {"type": "Point", "coordinates": makeLatLong(t)}
},
{
"type": "Feature",
"properties": {"name": "VAN AERT Wout"},
"id": 11,
"geometry": {"type": "Point", "coordinates": makeLatLong(t + 0.15)}
},
]
};
}
final _fills = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 0, // web currently only supports number ids
"properties": <String, dynamic>{'id': 0},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[151.178099204457737, -33.901517742631846],
[151.179025547977773, -33.872845324482071],
[151.147000529140399, -33.868230472039514],
[151.150838238009328, -33.883172899638311],
[151.14223647675135, -33.894158309528244],
[151.155999294764086, -33.904812805307806],
[151.178099204457737, -33.901517742631846]
],
[
[151.162657925954278, -33.879168932438581],
[151.155323416087612, -33.890737666431583],
[151.173659690754278, -33.897637567778119],
[151.162657925954278, -33.879168932438581]
]
]
}
},
{
"type": "Feature",
"id": 1,
"properties": <String, dynamic>{'id': 1},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[151.18735077583878, -33.891143558434102],
[151.197374605989864, -33.878357032551868],
[151.213021560372084, -33.886475683791488],
[151.204953599518745, -33.899463918807818],
[151.18735077583878, -33.891143558434102]
]
]
}
}
]
};
const _points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 2,
"properties": {
"type": "restaurant",
},
"geometry": {
"type": "Point",
"coordinates": [151.184913929732943, -33.874874486427181]
}
},
{
"type": "Feature",
"id": 3,
"properties": {
"type": "airport",
},
"geometry": {
"type": "Point",
"coordinates": [151.215730044667879, -33.874616048776858]
}
},
{
"type": "Feature",
"id": 4,
"properties": {
"type": "bakery",
},
"geometry": {
"type": "Point",
"coordinates": [151.228803547973598, -33.892188026142584]
}
},
{
"type": "Feature",
"id": 5,
"properties": {
"type": "college",
},
"geometry": {
"type": "Point",
"coordinates": [151.186470299174118, -33.902781145804774]
}
}
]
};