Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions example/lib/pages/map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MapControllerPage extends StatefulWidget {
}

class MapControllerPageState extends State<MapControllerPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
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);
Expand All @@ -31,7 +32,7 @@ class MapControllerPageState extends State<MapControllerPage> {
height: 80.0,
point: london,
builder: (ctx) => new Container(
key: new Key("blue"),
key: new Key("blue"),
child: new FlutterLogo(),
),
),
Expand All @@ -51,13 +52,14 @@ class MapControllerPageState extends State<MapControllerPage> {
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(
Expand Down Expand Up @@ -108,6 +110,22 @@ class MapControllerPageState extends State<MapControllerPage> {
);
},
),
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}',
),
));
},
),
],
),
),
Expand All @@ -118,7 +136,7 @@ class MapControllerPageState extends State<MapControllerPage> {
center: new LatLng(51.5, -0.09),
zoom: 5.0,
maxZoom: 5.0,
minZoom: 3.0
minZoom: 3.0,
),
layers: [
new TileLayerOptions(
Expand Down
4 changes: 3 additions & 1 deletion lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ abstract class MapController {
bool get ready;
Future<Null> get onReady;
LatLng get center;
LatLngBounds get bounds;
double get zoom;

factory MapController() => new MapControllerImpl();
Expand Down Expand Up @@ -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});
}
5 changes: 1 addition & 4 deletions lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ class MarkerLayer extends StatelessWidget {
var markers = <Widget>[];
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();

Expand All @@ -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;
}

Expand Down
28 changes: 27 additions & 1 deletion lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class MapControllerImpl implements MapController {

LatLng get center => _state.center;

LatLngBounds get bounds => _state.bounds;

double get zoom => _state.zoom;
}

Expand All @@ -48,6 +50,7 @@ class MapState {
double get zoom => _zoom;

LatLng _lastCenter;
LatLngBounds _lastBounds;
Point _pixelOrigin;
bool _initialized = false;

Expand All @@ -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);
Expand Down Expand Up @@ -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,
));
}
}

Expand All @@ -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;
Expand Down