-
Notifications
You must be signed in to change notification settings - Fork 5
/
map.dart
351 lines (313 loc) · 11.3 KB
/
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
import 'dart:collection';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
// FIXME: Be sure to set your own API key here. You can register for a free one at https://client.stadiamaps.com/.
const apiKey = "YOUR-API-KEY";
enum OfflineDataState { unknown, downloaded, downloading, notDownloaded }
class MapPage extends StatelessWidget {
const MapPage({super.key});
@override
Widget build(BuildContext context) {
return const Map();
}
}
class Map extends StatefulWidget {
const Map({super.key});
@override
State createState() => MapState();
}
class MapState extends State<Map> {
MapLibreMapController? mapController;
static const clusterLayer = "clusters";
static const unclusteredPointLayer = "unclustered-point";
OfflineDataState offlineDataState = OfflineDataState.unknown;
double? downloadProgress;
@override
void dispose() {
mapController?.onFeatureTapped.remove(_onFeatureTapped);
super.dispose();
}
void _onMapCreated(MapLibreMapController controller) async {
mapController = controller;
// Event listener that fires for the cluster layer (not due to an explicit
// filter; only a consequence of the current mix of layers used).
controller.onFeatureTapped.add(_onFeatureTapped);
// Determine if we have data stored offline. Note that this is a fairly
// crude check, and if you are dealing with multiple styles or regions,
// you will want to do something a bit more advanced.
final result = await getListOfRegions();
setState(() {
if (result.isEmpty) {
offlineDataState = OfflineDataState.notDownloaded;
} else {
offlineDataState = OfflineDataState.downloaded;
}
});
}
void _onStyleLoadedCallback() async {
const sourceId = "locations";
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("Style loaded"),
backgroundColor: Theme.of(context).primaryColor,
duration: const Duration(seconds: 1),
));
// Alternate form when using hard-coded local data; load this however you like
// await addClusteredPointSource(sourceId, {
// "type": "FeatureCollection",
// "features": [
// {
// "type": "Feature",
// "geometry": {
// "type": "Point",
// "coordinates": [-77.03238901390978, 38.913188059745586]
// },
// "properties": {"title": "Washington, DC"}
// },
// {
// "type": "Feature",
// "geometry": {
// "type": "Point",
// "coordinates": [-122.414, 37.776]
// },
// "properties": {"title": "San Francisco"}
// }
// ]
// });
await addClusteredPointSource(sourceId,
"https://maplibre.org/maplibre-gl-js/docs/assets/earthquakes.geojson");
await addClusteredPointLayers(sourceId);
}
// Logic for interacting with clusters on iOS.
// See bug report: https://github.com/m0nac0/flutter-maplibre-gl/issues/160
void _onFeatureTapped(
dynamic featureId, Point<double> point, LatLng coords) async {
var features =
await mapController?.queryRenderedFeatures(point, [clusterLayer], null);
if (features?.isNotEmpty ?? false) {
// Naive zoom += 2. There is a `getClusterExpansionZoom` method
// on sources, but the Flutter wrapper does not actually expose
// sources at the moment so we're just falling back to a simple
// approach.
mapController!.animateCamera(CameraUpdate.newLatLngZoom(
coords, mapController!.cameraPosition!.zoom + 2));
}
}
// This method handles interaction with the actual earthquake points on iOS.
// See bug report: https://github.com/m0nac0/flutter-maplibre-gl/issues/160
void _onMapClick(Point<double> point, LatLng coordinates) async {
var messenger = ScaffoldMessenger.of(context);
var color = Theme.of(context).primaryColor;
var features = await mapController?.queryRenderedFeatures(
point, [unclusteredPointLayer], null);
if (features?.isNotEmpty ?? false) {
var feature = HashMap.from(features!.first);
messenger.showSnackBar(SnackBar(
content: Text("Magnitude ${feature["properties"]["mag"]} earthquake"),
backgroundColor: color,
duration: const Duration(seconds: 3),
));
}
}
// Adds a data source to the map via a GeoJSON layer. The data is assumed
// to be a PointCollection
Future<void>? addClusteredPointSource(String sourceId, Object? data) {
return mapController?.addSource(
sourceId, GeojsonSourceProperties(data: data, cluster: true));
}
Future<void> addClusteredPointLayers(String sourceId) async {
await mapController?.addCircleLayer(
sourceId,
clusterLayer,
const CircleLayerProperties(circleColor: [
"step",
["get", "point_count"],
"#51bbd6",
100,
"#f1f075",
750,
"#f28cb1"
], circleRadius: [
"step",
["get", "point_count"],
20,
100,
30,
750,
40
]),
filter: ["has", "point_count"]);
await mapController?.addSymbolLayer(
sourceId,
"cluster-count",
const SymbolLayerProperties(
// NOTE: I would expect to be able to do something like "{point_count_abbreviated}", but this breaks on Android
textField: [Expressions.get, "point_count_abbreviated"],
textFont: ["Open Sans Regular"]),
filter: ["has", "point_count"]);
await mapController?.addCircleLayer(
sourceId,
unclusteredPointLayer,
const CircleLayerProperties(
circleColor: "#11b4da",
circleRadius: 8,
circleStrokeWidth: 1,
circleStrokeColor: "#fff"),
filter: [
"!",
["has", "point_count"]
]);
}
@override
Widget build(BuildContext context) {
final Widget child;
switch (offlineDataState) {
case OfflineDataState.downloaded:
child = const Icon(Icons.delete);
break;
case OfflineDataState.notDownloaded:
child = const Icon(Icons.download_for_offline_outlined);
break;
case OfflineDataState.downloading:
case OfflineDataState.unknown:
// Indeterminate progress indicator
child = CircularProgressIndicator(
value: downloadProgress,
color: Colors.white,
);
break;
}
final Widget? actionButton;
if (kIsWeb) {
// Offline tiles are not supported in the browser
actionButton = null;
} else {
actionButton =
FloatingActionButton(onPressed: _actionButtonPressed, child: child);
}
return Scaffold(
body: MapLibreMap(
styleString: _mapStyleUrl(),
myLocationEnabled: true,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
onMapCreated: _onMapCreated,
onStyleLoadedCallback: _onStyleLoadedCallback,
onMapClick: _onMapClick,
trackCameraPosition: true,
),
floatingActionButton: actionButton,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
String _mapStyleUrl() {
const styleUrl =
"https://tiles.stadiamaps.com/styles/alidade_smooth_dark.json";
return "$styleUrl?api_key=$apiKey";
}
void _actionButtonPressed() async {
switch (offlineDataState) {
case OfflineDataState.downloaded:
_deleteOfflineRegion();
break;
case OfflineDataState.notDownloaded:
await _downloadOfflineRegion();
break;
case OfflineDataState.downloading:
case OfflineDataState.unknown:
return;
}
}
Future<OfflineRegion?> _downloadOfflineRegion() async {
setState(() {
offlineDataState = OfflineDataState.downloading;
});
try {
// Bounding box around Manhattan. Note that this will consume
// approximately 200 API credits.
final bounds = LatLngBounds(
southwest: const LatLng(40.69, -74.03),
northeast: const LatLng(40.84, -73.86),
);
final regionDefinition = OfflineRegionDefinition(
bounds: bounds, mapStyleUrl: _mapStyleUrl(), minZoom: 0, maxZoom: 14);
final region = await downloadOfflineRegion(regionDefinition,
metadata: {
'name': 'Manhattan',
},
onEvent: _onDownloadEvent);
return region;
} on Exception catch (_) {
setState(() {
offlineDataState = OfflineDataState.notDownloaded;
downloadProgress = null;
});
return null;
}
}
void _onDownloadEvent(DownloadRegionStatus status) {
// Event listener for download progress; MapLibre uses a repeated
// callback API, and the download command, while async, completes early.
if (status is Success) {
setState(() {
offlineDataState = OfflineDataState.downloaded;
downloadProgress = null;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("Manhattan Downloaded for Offline Use"),
backgroundColor: Theme.of(context).primaryColor,
duration: const Duration(seconds: 3),
));
} else if (status is Error) {
setState(() {
offlineDataState = OfflineDataState.notDownloaded;
downloadProgress = null;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("Data Downloaded Failed!"),
backgroundColor: Theme.of(context).colorScheme.error,
duration: const Duration(seconds: 3),
));
} else if (status is InProgress) {
setState(() {
offlineDataState = OfflineDataState.downloading;
downloadProgress = status.progress / 100;
});
}
}
void _deleteOfflineRegion() async {
setState(() {
offlineDataState = OfflineDataState.unknown;
});
final regions = await getListOfRegions();
for (final region in regions) {
// NOTE: The term "delete" here is a bit of a misnomer. From the docs:
//
// When you remove an offline pack, any resources that are required by
// that pack, but not other packs, become eligible for deletion from
// offline storage. Because the backing store used for offline storage
// is also used as a general purpose cache for map resources, such
// resources may not be immediately removed if the implementation
// determines that they remain useful for general performance of the map.
//
// Ambient cache controls also exist, but these are not currently wrapped
// for Flutter. This is not normally an issue, and the storage engine will
// eventually clear these tiles out.
//
// Source: https://maplibre.org/maplibre-gl-native/ios/api/Classes/MGLOfflineStorage.html#/c:objc(cs)MGLOfflineStorage(im)removePack:withCompletionHandler:
await deleteOfflineRegion(
region.id,
);
}
setState(() {
offlineDataState = OfflineDataState.notDownloaded;
});
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("Offline data marked for removal"),
backgroundColor: Theme.of(context).primaryColor,
duration: const Duration(seconds: 1),
));
}
}
}