在 Flutter 的开放街道地图中旋转指定经度/纬度角的 ImageOverlay

问题描述 投票:0回答:1

我在 Flutter 中使用 flutter_map 放置了一张图像,该图像使用西北/东南角经度/纬度定位。我需要将此图像围绕其中心旋转指定的度数 - 我在 Google 地图中通过简单地使用 CSS 旋转图像 div 来完成此操作,这对于我们的目的来说效果很好。 Flutter 可以让我旋转 OverlayImageLayer,但不能旋转单个 overrideImage。

据我了解,flutter_map 使用第三个 lng/lat 来指定左下(SW)边界框角来控制旋转,并且可能还需要其他两个点的新坐标。

简而言之,我需要能够指定一个未旋转的初始边界框,然后在旋转指定度数后重新计算角位置。

flutter openstreetmap fluttermap
1个回答
0
投票

我找到了一个解决方案,归功于这个答案 - 每个角点都应该围绕中心点旋转,这会给出转换后的LatLngs,前提是您不重叠极点或跨越国际日期变更线。

  LatLng rotateLatLng(LatLng coord, LatLng centre, double degRotation) {
    // Make this constant so it doesn't have to be repeatedly recalculated
    const piDiv180 = maths.pi / 180;

    // Convert the input angle to radians
    final r = (0-degRotation) * piDiv180;

    // Create local variables using appropriate nomenclature
    final x = coord.longitude;
    final y = coord.latitude;
    final mx = centre.longitude;
    final my = centre.latitude;

    // Offset input point by the midpoint so the midpoint becomes the origin
    final ox = x - mx;
    final oy = y - my;

    // Cache trig results because trig is expensive
    final cosr = maths.cos(r);
    final sinr = maths.sin(r);

    // Perform rotation
    final dx = ox * cosr - oy * sinr;
    final dy = ox * sinr + oy * cosr;

    // Undo the offset
    return LatLng(dy + my, dx + mx);
  }
© www.soinside.com 2019 - 2024. All rights reserved.