google-maps - 如何在 Flutter 中限制 Google map 的边界?

标签 google-maps flutter dart google-maps-flutter

我正在使用 google_maps_flutter 包,我需要将 map 的可滚动区域限制为特定区域。我有 SW 和 NE 角,但我不知道怎么做。

我已经尝试了下面的代码,但它不起作用。

uniCampusSW 和 uniCampusNE 都是 LatLngs。

_userLocation == null // If user location has not been found
          ? Center(
              // Display Progress Indicator
              child: CircularProgressIndicator(
                backgroundColor: UniColors.primaryColour[500],
              ),
            )
          : GoogleMap(
              // Show Campus Map
              onMapCreated: _onMapCreated,
              initialCameraPosition: // required parameter that sets the starting camera position. Camera position describes which part of the world you want the map to point at.
                  CameraPosition(
                      target: _userLocation, zoom: defaultZoom, tilt: 0.0),
              scrollGesturesEnabled: true,
              tiltGesturesEnabled: true,
              trafficEnabled: false,
              compassEnabled: true,
              rotateGesturesEnabled: true,
              myLocationEnabled: true,
              mapType: _currentMapType,
              zoomGesturesEnabled: true,
              cameraTargetBounds: new CameraTargetBounds(
                new LatLngBounds(
                  northeast: UniCampusNE,
                  southwest: UniCampusSW,
                ),
              ),
            ),

这是我得到的错误

/flutter (12525): The following assertion was thrown building TheMap(dirty, state: _TheMapState#a8840): I/flutter (12525): 'package:google_maps_flutter/src/location.dart': Failed assertion: line 68 pos 16: I/flutter (12525): 'southwest.latitude <= northeast.latitude': is not true.



谢谢

最佳答案

你好 friend !
零安全解决方案
注意错误:'southwest.latitude <= northeast.latitude': 不是真的。
根据文档,链接如下:
https://pub.dev/documentation/google_maps_flutter_platform_interface/2.1.2/google_maps_flutter_platform_interface/LatLngBounds-class.html
有必要检查这些值是否不是静态的,因为southwest.latitude 必须小于或等于northeast.latitude,而northeast.longitude 必须小于或等于southwest.longitude。

LatLng? UniCampusNE;
LatLng? UniCampusSW;

var nLat, nLon, sLat, sLon;

if (UniCampusSW!.latitude <= UniCampusNE!.latitude) {

  sLat = UniCampusSW.latitude;
  nLat = UniCampusNE.latitude;

}else{

  sLat = UniCampusNE.latitude;
  nLat = UniCampusSW.latitude;

}
if (UniCampusSW.longitude <= UniCampusNE.longitude) {

  sLon = UniCampusSW.longitude;
  nLon = UniCampusNE.longitude;

}else{

  sLon = UniCampusNE.longitude;
  nLon = UniCampusSW.longitude;
}
在 cameraTargetBounds: 你可以使用你创建的变量:
cameraTargetBounds: CameraTargetBounds(
LatLngBounds(
northeast: LatLng(nLat, nLon),
southwest: LatLng(sLat, sLon),
),

关于google-maps - 如何在 Flutter 中限制 Google map 的边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627546/

相关文章:

windows-services - 如何将 DART 应用程序作为 Windows 服务运行?

ios - 无法识别的选择器发送到实例.. MapView 委托(delegate)的 'NSInvalidArgumentException'

javascript - 谷歌地图 div 区域中仅显示带有缩放控制的白色

java - Android 许可证状态未知且出现奇怪的问题

Android Studio 2022.3.1(Mac Intel)无法创建Flutter项目

google-maps - 从 google maps flutter 中删除右下角按钮

dart - 如何在 Dart 中创建空 map

google-maps - flutter 中更新Google Maps AlertDialog中的标记

angular - 将 DrawingManager 与 @angular/google-maps 一起使用

google-maps - Google map javascript API - 应用 KML 层后 map 对象重新居中。我该如何防止这种情况?