【问题标题】:Flutter LatLngBounds not showing accurate placeFlutter LatLngBounds 未显示准确的位置
【发布时间】:2020-08-26 14:31:24
【问题描述】:

我正在尝试使用我的颤振谷歌地图将我的所有标记显示到视口中。但它似乎不适用于我的情况。到目前为止,我已经尝试过:

    _controller.animateCamera(CameraUpdate.newLatLngBounds(
            LatLngBounds(
              southwest: LatLng(23.785182, 90.330702),
              northeast: LatLng(24.582782, 88.821163),
            ),
            100
        ));

    LatLngBounds boundsFromLatLngList(List<LatLng> list) {
    assert(list.isNotEmpty);
    double x0, x1, y0, y1;
    for (LatLng latLng in list) {
      if (x0 == null) {
        x0 = x1 = latLng.latitude;
        y0 = y1 = latLng.longitude;
      } else {
        if (latLng.latitude > x1) x1 = latLng.latitude;
        if (latLng.latitude < x0) x0 = latLng.latitude;
        if (latLng.longitude > y1) y1 = latLng.longitude;
        if (latLng.longitude < y0) y0 = latLng.longitude;
      }
    }
    return LatLngBounds(northeast: LatLng(x1, y1), southwest: LatLng(x0, y0));
  }

正如我所见,它总是显示北大西洋的地图 有没有关于这个问题的解决方案,或者它刚刚在 Flutter 中开发?提前致谢

【问题讨论】:

  • 我也有同样的问题。在 iOS 上它工作正常,但在 android 上 CameraUpdate.newLatLngBounds 总是缩小到最大并显示海洋中部。无论您作为坐标传递什么。
  • @ZigmārsDzērve 我在 android 上遇到了完全相同的问题,iOS 似乎工作正常。您是否设法找到解决方法?
  • @ChinmayDabke 是的,但它仅适用于 Android。我计算了我的点之间的中心位置,然后计算了缩放级别,并做了正常的正常 _controller.animateCamera(u2);当缩放级别和中心位置已知时。这是一项不必要的工作,但它可以解决谷歌地图目前存在的问题。

标签: flutter fitbounds


【解决方案1】:

当我使用 CameraUpdate.newLatLngBounds 为地图制作动画时,我在 Android 上遇到了完全相同的问题(在 iOS 上运行良好)。它在设置边界后立即重新定位到北太平洋,不确定是什么原因造成的,但这里有一个解决方法 -

不用LatLngBounds设置地图位置,你可以计算你想要设置的边界的中心

// the bounds you want to set
LatLngBounds bounds = LatLngBounds(
  southwest: LatLng(23.785182, 90.330702),
  northeast: LatLng(24.582782, 88.821163),
);
// calculating centre of the bounds
LatLng centerBounds = LatLng(
  (bounds.northeast.latitude + bounds.southwest.latitude)/2,
  (bounds.northeast.longitude + bounds.southwest.longitude)/2
);

// setting map position to centre to start with
controller.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
  target: centerBounds,
  zoom: 17,
)));
zoomToFit(controller, bounds, centerBounds);

将地图位置设置为边界中心(并放大)后,您需要继续缩小,直到可见地图区域覆盖您要设置的边界。您可以使用controller.getVisibleRegion() 获取可见地图区域。这是实现 -

Future<void> zoomToFit(GoogleMapController controller, LatLngBounds bounds, LatLng centerBounds) async {
  bool keepZoomingOut = true;

  while(keepZoomingOut) {
    final LatLngBounds screenBounds = await controller.getVisibleRegion();
    if(fits(bounds, screenBounds)){
      keepZoomingOut = false;
      final double zoomLevel = await controller.getZoomLevel() - 0.5;
      controller.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: centerBounds,
        zoom: zoomLevel,
      )));
      break;
    }
    else {
      // Zooming out by 0.1 zoom level per iteration
      final double zoomLevel = await controller.getZoomLevel() - 0.1;
      controller.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: centerBounds,
        zoom: zoomLevel,
      )));
    }
  }
}

bool fits(LatLngBounds fitBounds, LatLngBounds screenBounds) {
  final bool northEastLatitudeCheck = screenBounds.northeast.latitude >= fitBounds.northeast.latitude;
  final bool northEastLongitudeCheck = screenBounds.northeast.longitude >= fitBounds.northeast.longitude;

  final bool southWestLatitudeCheck = screenBounds.southwest.latitude <= fitBounds.southwest.latitude;
  final bool southWestLongitudeCheck = screenBounds.southwest.longitude <= fitBounds.southwest.longitude;

  return northEastLatitudeCheck && northEastLongitudeCheck && southWestLatitudeCheck && southWestLongitudeCheck;
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题:

    我并没有真正给出西南和东北坐标,但是: 西北和东南。 iOS 正常处理此问题,但 android 放大了大西洋。

    修复后:

    final highestLat = points.map((e) => e.latitude).reduce(max);
    final highestLong = points.map((e) => e.longitude).reduce(max);
    final lowestLat = points.map((e) => e.latitude).reduce(min);
    final lowestLong = points.map((e) => e.longitude).reduce(min);
    
    final lowestLatLowestLong = LatLng(lowestLat, lowestLong);
    final highestLatHighestLong = LatLng(highestLat, highestLong);
    final getRouteBoundsCameraUpdate = CameraUpdate.newLatLngBounds(LatLngBounds(southwest: lowestLatLowestLong, northeast: highestLatHighestLong), 25.0);
    

    【讨论】:

      【解决方案3】:

      我也遇到了这个问题,尝试将southwest 值换成northeast

      【讨论】:

        【解决方案4】:

        我发现这种方式非常适合我。

        Future<void> updateCameraLocation(
          LatLng source,
          LatLng destination,
          GoogleMapController mapController,
        ) async {
          if (mapController == null) return;
        
          LatLngBounds bounds;
        
          if (source.latitude > destination.latitude &&
              source.longitude > destination.longitude) {
            bounds = LatLngBounds(southwest: destination, northeast: source);
          } else if (source.longitude > destination.longitude) {
            bounds = LatLngBounds(
                southwest: LatLng(source.latitude, destination.longitude),
                northeast: LatLng(destination.latitude, source.longitude));
          } else if (source.latitude > destination.latitude) {
            bounds = LatLngBounds(
                southwest: LatLng(destination.latitude, source.longitude),
                northeast: LatLng(source.latitude, destination.longitude));
          } else {
            bounds = LatLngBounds(southwest: source, northeast: destination);
          }
        
          CameraUpdate cameraUpdate = CameraUpdate.newLatLngBounds(bounds, 70);
        
          return checkCameraLocation(cameraUpdate, mapController);
        }
        
        Future<void> checkCameraLocation(
            CameraUpdate cameraUpdate, GoogleMapController mapController) async {
          mapController.animateCamera(cameraUpdate);
          LatLngBounds l1 = await mapController.getVisibleRegion();
          LatLngBounds l2 = await mapController.getVisibleRegion();
        
          if (l1.southwest.latitude == -90 || l2.southwest.latitude == -90) {
            return checkCameraLocation(cameraUpdate, mapController);
          }
        }
        

        并通过这一行使用:

        await updateCameraLocation(source, destination, controller);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多