Flutter 贴图-按一定百分比/像素放大LatLngBounds

sshcrbum  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(185)

我有一个带有一些簇的Map,但是当我使用LatLngBounds将Map居中时,簇被剪切了。我需要添加一个填充以确保所有簇都可见。我尝试使用这个答案。它有时会工作,但当缩放很高时就不行了,我不明白为什么(在这种情况下,填充真的太大了)。我使用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);
    }

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

3z6pesqy

3z6pesqy1#

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

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

字符串
你不需要计算缩放级别。

相关问题