-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmap.dart
More file actions
395 lines (364 loc) · 13.4 KB
/
Copy pathmap.dart
File metadata and controls
395 lines (364 loc) · 13.4 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2022-2025 Ilya Zverev
// This file is a part of Every Door, distributed under GPL v3 or later version.
// Refer to LICENSE file and https://www.gnu.org/licenses/gpl-3.0.html for details.
import 'dart:async';
import 'dart:math' show min, max;
import 'package:eval_annotation/eval_annotation.dart';
import 'package:every_door/constants.dart';
import 'package:every_door/helpers/geometry/closest_points.dart';
import 'package:every_door/helpers/multi_icon.dart';
import 'package:every_door/providers/overlays.dart';
import 'package:every_door/widgets/pin_marker.dart';
import 'package:every_door/providers/editor_settings.dart';
import 'package:every_door/providers/geolocation.dart';
import 'package:every_door/providers/editor_mode.dart';
import 'package:every_door/providers/location.dart';
import 'package:every_door/screens/settings.dart';
import 'package:every_door/widgets/attribution.dart';
import 'package:every_door/widgets/loc_marker.dart';
import 'package:every_door/widgets/map_button.dart';
import 'package:every_door/widgets/walkpath.dart';
import 'package:every_door/widgets/zoom_buttons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart' show LatLng;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:every_door/generated/l10n/app_localizations.dart'
show AppLocalizations;
import '../providers/cur_imagery.dart';
@Bind()
class CustomMapController {
Function(Iterable<LatLng>)? zoomListener;
MapController? mapController;
GlobalKey? mapKey;
void setLocation({LatLng? location, double? zoom}) {
if (mapController != null) {
mapController!.move(location ?? mapController!.camera.center,
zoom ?? mapController!.camera.zoom);
}
}
void zoomToFit(Iterable<LatLng> locations) {
if (locations.isNotEmpty) {
if (zoomListener != null) zoomListener!(locations);
}
}
}
/// General map widget for every map in Every Door. Encloses layer management,
/// interaction, additional buttons etc etc.
@Bind()
class CustomMap extends ConsumerStatefulWidget {
final void Function(LatLng, double Function(LatLng))? onTap;
final CustomMapController? controller;
final List<Widget> layers;
final List<MapButton> buttons;
final bool drawZoomButtons;
/// When there is a floating button on the screen, zoom buttons
/// need to be moved higher.
final bool hasFloatingButton;
final bool drawStandardButtons;
final bool drawPinMarker;
final bool faintWalkPath;
final bool interactive;
final bool track;
final bool onlyOSM;
final bool allowRotation;
final bool updateState;
final bool switchToNavigate;
const CustomMap({
super.key,
this.onTap,
this.controller,
this.layers = const [],
this.buttons = const [],
this.drawZoomButtons = true,
this.hasFloatingButton = false,
this.drawStandardButtons = true,
this.drawPinMarker = true,
this.faintWalkPath = true,
this.interactive = true,
this.track = true,
this.onlyOSM = false,
this.allowRotation = true,
this.switchToNavigate = true,
this.updateState = false,
});
@override
ConsumerState createState() => _CustomMapState();
}
class _CustomMapState extends ConsumerState<CustomMap> {
static const kMapZoom = 17.0;
final MapController _controller = MapController();
final _mapKey = GlobalKey();
LatLng? _center;
int _rotation = 0;
StreamSubscription<MapEvent>? mapSub;
@override
void initState() {
super.initState();
if (widget.controller != null) {
widget.controller!.zoomListener = onControllerZoom;
widget.controller!.mapKey = _mapKey;
}
}
@override
void dispose() {
widget.controller?.mapController = null;
widget.controller?.mapKey = null;
mapSub?.cancel();
super.dispose();
}
void onMapReady() {
if (widget.updateState) {
mapSub = _controller.mapEventStream.listen(onMapEvent);
}
widget.controller?.mapController = _controller;
_center = _controller.camera.center;
setState(() {});
}
void onMapEvent(MapEvent event) {
bool fromController = event.source == MapEventSource.mapController ||
event.source == MapEventSource.nonRotatedSizeChange;
if (event is MapEventWithMove) {
if (!fromController) {
ref.read(trackingProvider.notifier).disable();
ref.read(zoomProvider.notifier).update(event.camera.zoom);
if (widget.switchToNavigate) {
final bool isNavigating = ref.read(navigationModeProvider);
if (isNavigating) {
if (event.camera.zoom > kEditMinZoom) {
// Switch navigation mode off
ref.read(navigationModeProvider.notifier).disable();
}
} else if (event.camera.zoom < kEditMinZoom) {
// Switch navigation mode on
ref.read(navigationModeProvider.notifier).enable();
ref.read(rotationProvider.notifier).reset();
}
}
} else {
ref
.read(visibleBoundsProvider.notifier)
.update(event.camera.visibleBounds);
}
if (event.camera.center != _center) {
setState(() {
_center = _controller.camera.center;
});
}
if (event.camera.rotation != _rotation) {
setState(() {
_rotation = event.camera.rotation.round();
});
}
} else if (event is MapEventMoveEnd) {
if (!fromController) {
ref.read(effectiveLocationProvider.notifier).set(event.camera.center);
}
ref
.read(visibleBoundsProvider.notifier)
.update(event.camera.visibleBounds);
} else if (event is MapEventRotateEnd) {
if (event.source != MapEventSource.mapController) {
double rotation = _controller.camera.rotation;
while (rotation > 200) rotation -= 360;
while (rotation < -200) rotation += 360;
if (rotation.abs() < kRotationThreshold) {
ref.read(rotationProvider.notifier).reset();
_controller.rotate(0.0);
} else {
ref.read(rotationProvider.notifier).update(rotation);
}
}
}
}
double _calculateZoom(Iterable<LatLng> locations, EdgeInsets padding) {
// Add a virtual location to keep center.
// Here we don't reproject, since on low zooms Mercator could be considered equirectandular.
// Taking first 9, for we display only 9.
final bounds = LatLngBounds.fromPoints(locations.take(9).toList());
final center = _controller.camera.center;
final dlat = max(
(bounds.north - center.latitude).abs(),
(bounds.south - center.latitude).abs(),
);
final dlon = max(
(bounds.east - center.longitude).abs(),
(bounds.west - center.longitude).abs(),
);
final newBounds = LatLngBounds(
LatLng(center.latitude - dlat, center.longitude - dlon),
LatLng(center.latitude + dlat, center.longitude + dlon),
);
return CameraFit.bounds(
bounds: newBounds, padding: padding, maxZoom: kMapZoom + 1)
.fit(_controller.camera)
.zoom;
}
void onControllerZoom(Iterable<LatLng> locations) {
const kPadding = EdgeInsets.all(12.0);
const kZoomThreshold = 0.2;
const kTooCloseThreshold = 10.0; // meters. I know, bad.
double zoom = _calculateZoom(locations, kPadding);
if (zoom < kMapZoom - 1 && locations.length >= 6) {
// When outliers are too far, we can skip them I guess.
zoom = _calculateZoom(locations.take(locations.length - 2), kPadding);
}
final curZoom = _controller.camera.zoom;
double maxZoomHere = kMapZoom;
if (zoom > kMapZoom && zoom > curZoom) {
// Overzoom only if points are too close.
if (closestPairDistance(locations) <= kTooCloseThreshold) maxZoomHere++;
}
if (zoom < kMapZoom - 1)
zoom = min(curZoom, kMapZoom - 1);
else if (zoom > maxZoomHere) zoom = max(curZoom, maxZoomHere);
if ((zoom - curZoom).abs() >= kZoomThreshold) {
_controller.move(_controller.camera.center, zoom);
ref.read(zoomProvider.notifier).update(zoom);
}
}
void onMapTap(TapPosition pos, LatLng location) {
final locationPx = _controller.camera.latLngToScreenOffset(location);
double distanceToLocation(LatLng loc2) {
return (locationPx - _controller.camera.latLngToScreenOffset(loc2))
.distance;
}
if (widget.onTap != null) {
widget.onTap!(location, distanceToLocation);
}
}
@override
Widget build(BuildContext context) {
final LatLng? trackLocation = ref.watch(geolocationProvider);
// TODO: move those two to tracking provider
if (widget.track) {
// When tracking location, move map and notify the poi list.
ref.listen<LatLng?>(geolocationProvider, (_, LatLng? location) {
if (location != null && ref.watch(trackingProvider)) {
_controller.move(location, _controller.camera.zoom);
ref.read(effectiveLocationProvider.notifier).set(location);
}
});
}
// When turning the tracking on, move the map immediately.
ref.listen(trackingProvider, (_, bool newState) {
if (trackLocation != null && newState) {
_controller.move(trackLocation, _controller.camera.zoom);
ref.read(effectiveLocationProvider.notifier).set(trackLocation);
}
});
ref.watch(geolocationProvider); // not using, but it triggers repaints
// Rotate the map according to the global rotation value.
ref.listen(rotationProvider, (_, double newValue) {
if ((newValue - _controller.camera.rotation).abs() >= 1.0) {
_controller.rotate(newValue);
}
});
// Update map position when changing panes.
ref.listen(effectiveLocationProvider, (_, LatLng next) {
_controller.move(next, _controller.camera.zoom);
});
final imagery = ref
.watch(widget.onlyOSM ? baseImageryProvider : selectedImageryProvider);
final isNavigating = ref.watch(navigationModeProvider);
final leftHand = ref.watch(editorSettingsProvider).leftHand;
final loc = AppLocalizations.of(context)!;
return FlutterMap(
mapController: _controller,
key: _mapKey,
options: MapOptions(
initialCenter: ref.watch(effectiveLocationProvider),
initialRotation: ref.watch(rotationProvider),
initialZoom: ref.watch(zoomProvider),
minZoom: isNavigating ? kNavigateMinZoom : kEditMinZoom - 0.1,
maxZoom: isNavigating ? kEditMinZoom + 0.1 : kEditMaxZoom,
interactionOptions: InteractionOptions(
flags: !widget.interactive
? InteractiveFlag.none
: InteractiveFlag.all -
InteractiveFlag.flingAnimation -
(widget.allowRotation ? 0 : InteractiveFlag.rotate),
rotationThreshold: kRotationThreshold,
),
onMapReady: onMapReady,
onTap: widget.onTap == null ? null : onMapTap,
),
children: [
imagery.buildLayer(reset: true),
...ref
.watch(overlayImageryProvider)
.map((i) => i.buildLayer(reset: true)),
LocationMarkerWidget(),
WalkPathPolyline(faint: widget.faintWalkPath),
AttributionWidget(imagery),
...widget.layers,
if (widget.drawPinMarker &&
_center != null &&
(!ref.watch(trackingProvider) || trackLocation == null))
MarkerLayer(markers: [PinMarker(_center!)]),
if (widget.drawStandardButtons)
// Settings button
OverlayButtonWidget(
alignment: leftHand ? Alignment.topRight : Alignment.topLeft,
padding: EdgeInsets.symmetric(
horizontal: 0.0,
vertical: 10.0,
),
icon: MultiIcon.font(Icons.menu),
tooltip: loc.mapSettings,
onPressed: (_) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SettingsPage()),
);
},
),
MapButtonColumn(
alignment: leftHand ? Alignment.topLeft : Alignment.topRight,
buttons: [
if (widget.drawStandardButtons) ...[
// Tracking button
MapButton(
enabled: !ref.watch(trackingProvider) && trackLocation != null,
icon: MultiIcon.font(Icons.my_location),
tooltip: loc.mapLocate,
onPressed: (_) {
ref
.read(geolocationProvider.notifier)
.enableTracking(context);
},
),
// Rotation button
MapButton(
enabled: _rotation != 0,
child: Transform.rotate(
angle: _rotation.toDouble() / 180 * 3.14159,
child: Icon(
Icons.navigation_outlined,
size: 30,
color: Colors.black54,
),
),
tooltip: loc.mapStraight,
onPressed: (_) {
ref.read(rotationProvider.notifier).reset();
_controller.rotate(0.0);
_rotation = 0;
},
),
],
...widget.buttons,
],
safeRight: true,
),
if (widget.drawZoomButtons)
ZoomButtonsWidget(
alignment: leftHand ? Alignment.bottomLeft : Alignment.bottomRight,
padding: EdgeInsets.symmetric(
horizontal: 0.0,
vertical: widget.hasFloatingButton ? 100.0 : 20.0),
),
],
);
}
}