map: fixed move to marker

This commit is contained in:
Thibault Deckers 2021-08-19 20:41:32 +09:00
parent 2d1c350772
commit 1f192e58f2
3 changed files with 17 additions and 16 deletions

View file

@ -151,16 +151,16 @@ class _EntryGoogleMapState extends State<EntryGoogleMap> with WidgetsBindingObse
animation: _markerBitmapChangeNotifier, animation: _markerBitmapChangeNotifier,
builder: (context, child) { builder: (context, child) {
final markers = <Marker>{}; final markers = <Marker>{};
final onEntryTap = widget.onMarkerTap;
geoEntryByMarkerKey.forEach((markerKey, geoEntry) { geoEntryByMarkerKey.forEach((markerKey, geoEntry) {
final bytes = _markerBitmaps[markerKey]; final bytes = _markerBitmaps[markerKey];
if (bytes != null) { if (bytes != null) {
final latLng = LatLng(geoEntry.latitude!, geoEntry.longitude!); final point = LatLng(geoEntry.latitude!, geoEntry.longitude!);
markers.add(Marker( markers.add(Marker(
markerId: MarkerId(geoEntry.markerId!), markerId: MarkerId(geoEntry.markerId!),
consumeTapEvents: true,
icon: BitmapDescriptor.fromBytes(bytes), icon: BitmapDescriptor.fromBytes(bytes),
position: latLng, position: point,
onTap: onEntryTap != null ? () => onEntryTap(geoEntry) : null, onTap: () => widget.onMarkerTap?.call(geoEntry),
)); ));
} }
}); });
@ -241,11 +241,11 @@ class _EntryGoogleMapState extends State<EntryGoogleMap> with WidgetsBindingObse
await controller.animateCamera(CameraUpdate.zoomBy(amount)); await controller.animateCamera(CameraUpdate.zoomBy(amount));
} }
Future<void> _moveTo(LatLng latLng) async { Future<void> _moveTo(LatLng point) async {
final controller = _googleMapController; final controller = _googleMapController;
if (controller == null) return; if (controller == null) return;
await controller.animateCamera(CameraUpdate.newLatLng(latLng)); await controller.animateCamera(CameraUpdate.newLatLng(point));
} }
// `LatLng` used by `google_maps_flutter` is not the one from `latlong2` package // `LatLng` used by `google_maps_flutter` is not the one from `latlong2` package

View file

@ -60,7 +60,7 @@ class _EntryLeafletMapState extends State<EntryLeafletMap> with TickerProviderSt
bool get interactive => widget.interactive; bool get interactive => widget.interactive;
// duration should match the uncustomizable Google Maps duration // duration should match the uncustomizable Google Maps duration
static const _cameraAnimationDuration = Duration(milliseconds: 400); static const _cameraAnimationDuration = Duration(milliseconds: 600);
@override @override
void initState() { void initState() {
@ -123,10 +123,7 @@ class _EntryLeafletMapState extends State<EntryLeafletMap> with TickerProviderSt
return Marker( return Marker(
point: latLng, point: latLng,
builder: (context) => GestureDetector( builder: (context) => GestureDetector(
onTap: () { onTap: () => widget.onMarkerTap?.call(geoEntry),
widget.onMarkerTap?.call(geoEntry);
_moveTo(latLng);
},
child: widget.markerBuilder(markerKey), child: widget.markerBuilder(markerKey),
), ),
width: markerSize.width, width: markerSize.width,

View file

@ -77,7 +77,12 @@ class _MapPageState extends State<MapPage> {
onMarkerTap: (markerEntries) { onMarkerTap: (markerEntries) {
if (markerEntries.isEmpty) return; if (markerEntries.isEmpty) return;
final entry = markerEntries.first; final entry = markerEntries.first;
_selectedIndexNotifier.value = entries.indexOf(entry); final index = entries.indexOf(entry);
if (_selectedIndexNotifier.value != index) {
_selectedIndexNotifier.value = index;
} else {
_moveToEntry(entry);
}
}, },
), ),
), ),
@ -100,8 +105,7 @@ class _MapPageState extends State<MapPage> {
); );
} }
void _onThumbnailIndexChange() { void _onThumbnailIndexChange() => _moveToEntry(widget.entries[_selectedIndexNotifier.value]);
final position = widget.entries[_selectedIndexNotifier.value].latLng!;
_debouncer(() => _mapController.moveTo(position)); void _moveToEntry(AvesEntry entry) => _debouncer(() => _mapController.moveTo(entry.latLng!));
}
} }