Mark the place the map is pointed at, and make following work
Choosing a town moved the camera and then left the reader to work out which of the settlements now on screen was the one they asked for. A town has no precise position, so the map now marks its centre: a white disc under a coloured dot, drawn as a style layer so the native renderer keeps it pinned through every pan and zoom. The radar frames are inserted below it, because a band of rain must not paint over the one thing that says which town this is. A searched municipality becomes a PointTarget rather than being thrown away as "the whole region". It is marked, the recentre button returns to it, and the chip names it - but it is never persisted, so looking something up no longer costs the user the place their app opens on. That was a real defect: searching went through select(FreeTarget), which wrote null over the stored preference. Following the device was broken outright. MapLibre reports a camera animation the app started exactly as it reports the user grabbing the map, so engaging follow and then animating to the position cancelled the follow it had just started; the resulting FreeTarget then re-framed the whole region. Follow now moves the camera first and switches tracking on second, and FreeTarget no longer moves the camera as a reaction to the state changing - framing the region is an action, so panning away while following keeps the view the user panned to. Verified on the emulator with a fix in Turin: a searched town is marked and saveable, the app reopens on it with the marker in place, following centres on the blue dot, and panning stops the chase without yanking the map away. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
|
||||
import '../../core/region/geo.dart';
|
||||
|
||||
/// Marks the point the map is pointed at.
|
||||
///
|
||||
/// The blue dot answers "where am I". This answers "where is the place I
|
||||
/// chose": without it, picking a town moves the camera and then leaves the
|
||||
/// reader to guess which of the settlements now on screen was the one they
|
||||
/// asked for. A town has no precise position of its own, so what gets marked is
|
||||
/// its centre.
|
||||
///
|
||||
/// Drawn as a style layer rather than as a widget stacked over the map. The
|
||||
/// native renderer keeps a layer pinned to its coordinates through every pan,
|
||||
/// zoom and camera animation; a widget would have to be repositioned from Dart
|
||||
/// on every frame, one asynchronous coordinate conversion at a time, and would
|
||||
/// visibly swim behind the map while it moved.
|
||||
class PlaceMarker {
|
||||
PlaceMarker({required this.map, required this.color});
|
||||
|
||||
static const String sourceId = 'nuvolari-place-marker';
|
||||
|
||||
/// The lower of the two layers.
|
||||
///
|
||||
/// Public because layer order is a property of the whole map, not of this
|
||||
/// class: the radar frames are added *below* this id, which is what stops a
|
||||
/// band of rain from being painted over the marker.
|
||||
static const String haloLayerId = 'nuvolari-place-marker-halo';
|
||||
static const String dotLayerId = 'nuvolari-place-marker-dot';
|
||||
|
||||
final MapLibreMapController map;
|
||||
|
||||
/// Fill colour of the dot. The white ring underneath carries the contrast, so
|
||||
/// this only has to identify the app.
|
||||
final Color color;
|
||||
|
||||
bool _attached = false;
|
||||
GeoPoint? _point;
|
||||
|
||||
bool get isAttached => _attached;
|
||||
|
||||
/// The point currently marked, or null when nothing is.
|
||||
GeoPoint? get point => _point;
|
||||
|
||||
/// Serialises platform-channel work, so a target change that lands while the
|
||||
/// previous one is still in flight cannot apply out of order.
|
||||
Future<void> _pending = Future<void>.value();
|
||||
|
||||
/// Creates the source and layers, marking nothing yet.
|
||||
///
|
||||
/// Called before the radar overlay attaches so that the marker's layers are
|
||||
/// already in the style for the frames to be inserted underneath.
|
||||
Future<void> attach() => _serialise(() async {
|
||||
if (_attached) return;
|
||||
|
||||
await map.addGeoJsonSource(sourceId, _featureCollection(null));
|
||||
|
||||
// A white disc under a coloured dot. The radar palette runs through blues
|
||||
// and greens, and a marker in any single colour disappears into some part
|
||||
// of it; a ring of the background colour reads against all of them.
|
||||
await map.addCircleLayer(
|
||||
sourceId,
|
||||
haloLayerId,
|
||||
const CircleLayerProperties(
|
||||
circleRadius: 12.0,
|
||||
circleColor: '#FFFFFF',
|
||||
circleOpacity: 0.95,
|
||||
),
|
||||
enableInteraction: false,
|
||||
);
|
||||
await map.addCircleLayer(
|
||||
sourceId,
|
||||
dotLayerId,
|
||||
CircleLayerProperties(circleRadius: 6.0, circleColor: _hex(color)),
|
||||
enableInteraction: false,
|
||||
);
|
||||
|
||||
_attached = true;
|
||||
_point = null;
|
||||
});
|
||||
|
||||
/// Moves the marker to [point], or clears it when [point] is null.
|
||||
Future<void> show(GeoPoint? point) => _serialise(() async {
|
||||
if (!_attached) return;
|
||||
if (point == _point) return;
|
||||
|
||||
// Emptying the feature collection hides the marker without removing the
|
||||
// layers, which keeps them above the radar for the next time it is shown.
|
||||
await map.setGeoJsonSource(sourceId, _featureCollection(point));
|
||||
_point = point;
|
||||
});
|
||||
|
||||
/// Removes the layers and the source.
|
||||
Future<void> detach() => _serialise(() async {
|
||||
if (!_attached) return;
|
||||
|
||||
// Layers reference the source, so they go first. Each removal tolerates
|
||||
// failing: the style may already have dropped them on a reload, and the
|
||||
// goal is to end up with nothing rather than to prove what was there.
|
||||
for (final layer in <String>[dotLayerId, haloLayerId]) {
|
||||
try {
|
||||
await map.removeLayer(layer);
|
||||
} on Object {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await map.removeSource(sourceId);
|
||||
} on Object {
|
||||
// Nothing left to do; the state below is reset either way.
|
||||
}
|
||||
|
||||
_attached = false;
|
||||
_point = null;
|
||||
});
|
||||
|
||||
Map<String, dynamic> _featureCollection(GeoPoint? point) => <String, dynamic>{
|
||||
'type': 'FeatureCollection',
|
||||
'features': <Map<String, dynamic>>[
|
||||
if (point != null)
|
||||
<String, dynamic>{
|
||||
'type': 'Feature',
|
||||
'geometry': <String, dynamic>{
|
||||
'type': 'Point',
|
||||
'coordinates': <double>[point.longitude, point.latitude],
|
||||
},
|
||||
'properties': <String, dynamic>{},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
Future<void> _serialise(Future<void> Function() action) {
|
||||
final next = _pending.then((_) => action());
|
||||
// Swallowed for the chain's sake only: one platform-channel failure must
|
||||
// not deadlock every later update queued behind it.
|
||||
_pending = next.catchError((Object _) {});
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
/// The `#RRGGBB` string the style expects.
|
||||
String _hex(Color color) {
|
||||
final rgb = color.toARGB32() & 0xFFFFFF;
|
||||
return '#${rgb.toRadixString(16).padLeft(6, '0').toUpperCase()}';
|
||||
}
|
||||
Reference in New Issue
Block a user