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( diff --git a/lib/flutter_map.dart b/lib/flutter_map.dart index c43c08c87..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(); @@ -130,6 +131,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/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; } diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index a29fef056..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; } @@ -48,6 +50,7 @@ class MapState { double get zoom => _zoom; LatLng _lastCenter; + LatLngBounds _lastBounds; Point _pixelOrigin; bool _initialized = false; @@ -70,6 +73,8 @@ class MapState { LatLng get center => getCenter() ?? options.center; + LatLngBounds get bounds => getBounds(); + void _init() { _zoom = options.zoom; move(options.center, zoom); @@ -98,11 +103,16 @@ class MapState { _zoom = zoom; _lastCenter = center; + _lastBounds = _calculateBounds(); _pixelOrigin = getNewPixelOrigin(center); _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, + )); } } @@ -121,6 +131,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;