Flutter Map - 将 LatLngBounds 放大一定百分比/像素

Flutter Map - Enlarge LatLngBounds by a certains percent/pixel

提问人:Velliane 提问时间:11/14/2023 更新时间:11/14/2023 访问量:25

问:

我有一个带有一些聚类的地图,但是当我使用 LatLngBounds 将地图居中时,聚类被剪切了。我需要添加一个填充以确保所有集群都可见。 我尝试使用这个答案。它有时会起作用,但当变焦很高时就不行了,我不明白为什么(在这种情况下,填充真的很大)。 我正在使用flutter_map包。

这是我所做的(mapBoundsEnlargePixels现在是随机的,我试图找到正确的值):

    _latLngBounds = LatLngBounds.fromPoints(markers.map((e) => e.point).toList());
    final mapBoundsEnlargePixels = 24;

    const Crs crs = Epsg4326();
    final CustomPoint<double> northEast =
        crs.latLngToPoint(bounds.northEast, _mapController.zoom);
    final CustomPoint<double> southWest =
        crs.latLngToPoint(bounds.southWest, _mapController.zoom);

    // north east
    final northEastX = northEast.x + mapBoundsEnlargePixels;
    final northEastY = northEast.y - mapBoundsEnlargePixels;
   
    // south west
    final southWestX = southWest.x - mapBoundsEnlargePixels;
    final southWestY = southWest.y + mapBoundsEnlargePixels; 

    final LatLng? ne = crs.pointToLatLng(CustomPoint(northEastX, northEastY), _mapController.zoom);
    final LatLng? sw = crs.pointToLatLng(CustomPoint(southWestX, southWestY), _mapController.zoom);

    if (ne != null && sw != null) {
      _latLngBounds!.extendBounds(LatLngBounds(ne, sw));
      // Make small move on the map to fix tile not loaded when center to Bounds
      print(_mapController.zoom);
      _mapController
        ..fitBounds(bounds)
        ..move(_mapController.center, _mapController.zoom + 0.000001);
    }

如果你有一个解释,甚至是可以使用的其他方法,那将是有用的。 谢谢

飞镖 FlutterMap

评论


答:

1赞 Ubaid ur Rehman Soomro 11/14/2023 #1

最新版本flutter_map包允许您在设置地图边界后设置填充。

_mapController.fitCamera(CameraFit.bounds(bounds: bounds),padding: EdgeInsets.all(10));

您无需计算缩放级别。

评论

0赞 Velliane 11/16/2023
没错,我不是最新版本。我工作得很好,非常感谢!