From 236b3a30fa683df20072180212b1f65603768adc Mon Sep 17 00:00:00 2001 From: Victor Choueiri Date: Wed, 12 Sep 2018 01:15:36 +0300 Subject: [PATCH 1/5] Add bounds to MapState: - Calculate bounds on move - Cache in state - Add getter --- lib/src/map/map.dart | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index a29fef056..ad7d13070 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -48,6 +48,7 @@ class MapState { double get zoom => _zoom; LatLng _lastCenter; + LatLngBounds _lastBounds; Point _pixelOrigin; bool _initialized = false; @@ -70,6 +71,8 @@ class MapState { LatLng get center => getCenter() ?? options.center; + LatLngBounds get bounds => getBounds(); + void _init() { _zoom = options.zoom; move(options.center, zoom); @@ -98,6 +101,7 @@ class MapState { _zoom = zoom; _lastCenter = center; + _lastBounds = _calculateBounds(); _pixelOrigin = getNewPixelOrigin(center); _onMoveSink.add(null); @@ -121,6 +125,22 @@ class MapState { return layerPointToLatLng(_centerLayerPoint); } + LatLngBounds getBounds() { + if (_lastBounds != null) { + return _lastBounds; + } + + return _calculateBounds(); + } + + LatLngBounds _calculateBounds() { + var bounds = getPixelBounds(zoom); + return new LatLngBounds( + unproject(bounds.bottomLeft), + unproject(bounds.topRight), + ); + } + CenterZoom _getBoundsCenterZoom( LatLngBounds bounds, FitBoundsOptions options) { var paddingTL = options.padding; From 05eae83aa2e85a18033e63ba883e6ddcd26a24e4 Mon Sep 17 00:00:00 2001 From: Victor Choueiri Date: Wed, 12 Sep 2018 01:16:48 +0300 Subject: [PATCH 2/5] Add bounds to MapPosition --- lib/flutter_map.dart | 3 ++- lib/src/map/map.dart | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/flutter_map.dart b/lib/flutter_map.dart index c43c08c87..5eea1de5a 100644 --- a/lib/flutter_map.dart +++ b/lib/flutter_map.dart @@ -130,6 +130,7 @@ class FitBoundsOptions { class MapPosition { final LatLng center; + final LatLngBounds bounds; final double zoom; - MapPosition({this.center, this.zoom}); + MapPosition({this.center, this.bounds, this.zoom}); } diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index ad7d13070..54e023c63 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -106,7 +106,11 @@ class MapState { _onMoveSink.add(null); if (options.onPositionChanged != null) { - options.onPositionChanged(new MapPosition(center: center, zoom: zoom)); + options.onPositionChanged(new MapPosition( + center: center, + bounds: bounds, + zoom: zoom, + )); } } From 2a9e747b7a11223f14730a384a79cfb4212c9598 Mon Sep 17 00:00:00 2001 From: Victor Choueiri Date: Wed, 12 Sep 2018 01:17:16 +0300 Subject: [PATCH 3/5] Add bounds getter to MapController/MapControllerImpl --- lib/flutter_map.dart | 1 + lib/src/map/map.dart | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/flutter_map.dart b/lib/flutter_map.dart index 5eea1de5a..4c302324d 100644 --- a/lib/flutter_map.dart +++ b/lib/flutter_map.dart @@ -59,6 +59,7 @@ abstract class MapController { bool get ready; Future get onReady; LatLng get center; + LatLngBounds get bounds; double get zoom; factory MapController() => new MapControllerImpl(); diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index 54e023c63..faafe611a 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -36,6 +36,8 @@ class MapControllerImpl implements MapController { LatLng get center => _state.center; + LatLngBounds get bounds => _state.bounds; + double get zoom => _state.zoom; } From 1643ae0681277bbb86237ad7c0b7d95c6678433f Mon Sep 17 00:00:00 2001 From: Victor Choueiri Date: Wed, 12 Sep 2018 01:17:55 +0300 Subject: [PATCH 4/5] Use map.bounds in MarkerLayer --- lib/src/layer/marker_layer.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/src/layer/marker_layer.dart b/lib/src/layer/marker_layer.dart index 2d78600d1..d57a861dc 100644 --- a/lib/src/layer/marker_layer.dart +++ b/lib/src/layer/marker_layer.dart @@ -85,9 +85,6 @@ class MarkerLayer extends StatelessWidget { var markers = []; for (var markerOpt in this.markerOpts.markers) { var pos = map.project(markerOpt.point); - var bounds = map.getPixelBounds(map.zoom); - var latlngBounds = new LatLngBounds( - map.unproject(bounds.bottomLeft), map.unproject(bounds.topRight)); pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) - map.getPixelOrigin(); @@ -96,7 +93,7 @@ class MarkerLayer extends StatelessWidget { var pixelPosY = (pos.y - (markerOpt.height - markerOpt._anchor.top)).toDouble(); - if (!latlngBounds.contains(markerOpt.point)) { + if (!map.bounds.contains(markerOpt.point)) { continue; } From bdcbe1b5dc0fa4f9a3b3e822a3930e09a8523994 Mon Sep 17 00:00:00 2001 From: Victor Choueiri Date: Wed, 12 Sep 2018 01:18:10 +0300 Subject: [PATCH 5/5] Add example --- example/lib/pages/map_controller.dart | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/example/lib/pages/map_controller.dart b/example/lib/pages/map_controller.dart index e42ea964e..77266cf0c 100644 --- a/example/lib/pages/map_controller.dart +++ b/example/lib/pages/map_controller.dart @@ -13,6 +13,7 @@ class MapControllerPage extends StatefulWidget { } class MapControllerPageState extends State { + final GlobalKey _scaffoldKey = new GlobalKey(); static LatLng london = new LatLng(51.5, -0.09); static LatLng paris = new LatLng(48.8566, 2.3522); static LatLng dublin = new LatLng(53.3498, -6.2603); @@ -31,7 +32,7 @@ class MapControllerPageState extends State { height: 80.0, point: london, builder: (ctx) => new Container( - key: new Key("blue"), + key: new Key("blue"), child: new FlutterLogo(), ), ), @@ -51,13 +52,14 @@ class MapControllerPageState extends State { height: 80.0, point: paris, builder: (ctx) => new Container( - key: new Key("purple"), + key: new Key("purple"), child: new FlutterLogo(colors: Colors.purple), ), ), ]; return new Scaffold( + key: _scaffoldKey, appBar: new AppBar(title: new Text("MapController")), drawer: buildDrawer(context, MapControllerPage.route), body: new Padding( @@ -108,6 +110,22 @@ class MapControllerPageState extends State { ); }, ), + new MaterialButton( + child: new Text("Get Bounds"), + onPressed: () { + final bounds = mapController.bounds; + + _scaffoldKey.currentState.showSnackBar(new SnackBar( + content: new Text( + 'Map bounds: \n' + 'E: ${bounds.east} \n' + 'N: ${bounds.north} \n' + 'W: ${bounds.west} \n' + 'S: ${bounds.south}', + ), + )); + }, + ), ], ), ), @@ -118,7 +136,7 @@ class MapControllerPageState extends State { center: new LatLng(51.5, -0.09), zoom: 5.0, maxZoom: 5.0, - minZoom: 3.0 + minZoom: 3.0, ), layers: [ new TileLayerOptions(