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
7 changes: 7 additions & 0 deletions lib/src/core/bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class Bounds<T extends num> {
(b.max.y <= max.y);
}

bool containsPartialBounds(Bounds<T> 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)';
}
14 changes: 13 additions & 1 deletion lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<int>(
Expand All @@ -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;
}

Expand Down
14 changes: 13 additions & 1 deletion lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MapState {

LatLng _lastCenter;
LatLngBounds _lastBounds;
Bounds _lastPixelBounds;
CustomPoint _pixelOrigin;
bool _initialized = false;

Expand All @@ -84,6 +85,8 @@ class MapState {

LatLngBounds get bounds => getBounds();

Bounds get pixelBounds => getLastPixelBounds();

void _init() {
_zoom = options.zoom;
move(options.center, zoom);
Expand All @@ -103,6 +106,7 @@ class MapState {

_zoom = zoom;
_lastCenter = center;
_lastPixelBounds = getPixelBounds(_zoom);
_lastBounds = _calculateBounds();
_pixelOrigin = getNewPixelOrigin(center);
_onMoveSink.add(null);
Expand Down Expand Up @@ -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),
Expand Down