From 5255b301cb77e9db7003b0d313dd2709416cc42b Mon Sep 17 00:00:00 2001 From: lorenzo Date: Fri, 7 Jun 2019 15:48:56 +0200 Subject: [PATCH] #230 fix When the point is out of bounds, marker widget disappears --- lib/src/core/bounds.dart | 7 +++++++ lib/src/layer/marker_layer.dart | 14 +++++++++++++- lib/src/map/map.dart | 14 +++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/src/core/bounds.dart b/lib/src/core/bounds.dart index c19aa23d0..bcfc4015f 100644 --- a/lib/src/core/bounds.dart +++ b/lib/src/core/bounds.dart @@ -59,6 +59,13 @@ class Bounds { (b.max.y <= max.y); } + bool containsPartialBounds(Bounds b) { + return (b.min.x <= max.x) && + (b.max.x >= min.x) && + (b.min.y <= max.y) && + (b.max.y >= min.y); + } + @override String toString() => 'Bounds($min, $max)'; } diff --git a/lib/src/layer/marker_layer.dart b/lib/src/layer/marker_layer.dart index dec91a149..fa7a395b9 100644 --- a/lib/src/layer/marker_layer.dart +++ b/lib/src/layer/marker_layer.dart @@ -1,5 +1,6 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_map/flutter_map.dart'; +import 'package:flutter_map/src/core/bounds.dart'; import 'package:flutter_map/src/map/map.dart'; import 'package:latlong/latlong.dart'; @@ -93,6 +94,17 @@ class MarkerLayer extends StatelessWidget { MarkerLayer(this.markerOpts, this.map, this.stream); + bool _boundsContainsMarker(Marker marker) { + var pixelPoint = map.project(marker.point); + + final width = marker.width - marker.anchor.left; + final height = marker.height - marker.anchor.top; + + var sw = CustomPoint(pixelPoint.x + width, pixelPoint.y - height); + var ne = CustomPoint(pixelPoint.x - width, pixelPoint.y + height); + return map.pixelBounds.containsPartialBounds(Bounds(sw, ne)); + } + @override Widget build(BuildContext context) { return StreamBuilder( @@ -109,7 +121,7 @@ class MarkerLayer extends StatelessWidget { var pixelPosY = (pos.y - (markerOpt.height - markerOpt.anchor.top)).toDouble(); - if (!map.bounds.contains(markerOpt.point)) { + if (!_boundsContainsMarker(markerOpt)) { continue; } diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index c70312b09..53004701a 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -60,6 +60,7 @@ class MapState { LatLng _lastCenter; LatLngBounds _lastBounds; + Bounds _lastPixelBounds; CustomPoint _pixelOrigin; bool _initialized = false; @@ -84,6 +85,8 @@ class MapState { LatLngBounds get bounds => getBounds(); + Bounds get pixelBounds => getLastPixelBounds(); + void _init() { _zoom = options.zoom; move(options.center, zoom); @@ -103,6 +106,7 @@ class MapState { _zoom = zoom; _lastCenter = center; + _lastPixelBounds = getPixelBounds(_zoom); _lastBounds = _calculateBounds(); _pixelOrigin = getNewPixelOrigin(center); _onMoveSink.add(null); @@ -153,8 +157,16 @@ class MapState { return _calculateBounds(); } + Bounds getLastPixelBounds() { + if (_lastPixelBounds != null) { + return _lastPixelBounds; + } + + return getPixelBounds(zoom); + } + LatLngBounds _calculateBounds() { - var bounds = getPixelBounds(zoom); + var bounds = getLastPixelBounds(); return LatLngBounds( unproject(bounds.bottomLeft), unproject(bounds.topRight),