google-maps - 如何在 Flutter 中限制谷歌地图的边界?
问题描述
我正在使用 google_maps_flutter 包,我需要将地图的可滚动区域限制为特定区域。我有西南角和东北角,但我不知道该怎么做。
我已经尝试了下面的代码,但它不起作用。
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):在构建 TheMap(脏,状态:_TheMapState#a8840)时引发了以下断言:I/flutter(12525):'package:google_maps_flutter/src/location.dart':断言失败:第 68 行 pos 16: I/flutter (12525): 'southwest.latitude <= northeast.latitude': 不正确。
谢谢
解决方案
大家好!
具有零安全性的解决方案
请注意错误:'southwest.latitude <= northeast.latitude': 不正确。
根据文档,链接如下:
需要检查这些值是否不是静态的,因为swestern.latitude必须小于或等于northwest.latitude,并且eastern.longitude必须小于或等于southouth.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),
),
推荐阅读
- r - 激活/停用 R 中的循环
- youtube-api - 如何在不使用 You Tube Data API 的搜索功能的情况下获取频道正在进行的直播视频
- c# - 简单的 odata 客户端过滤器嵌套集合
- python - 单元测试不会运行测试
- azure - 如何在 Azure 函数中的 Configure(IWebJobsBuilder builder) 中获取 FunctionAppDirectory
- python - 递归函数只返回嵌套字典一个分支的结果
- google-places-api - 在哪里可以找到 address_components 类型的所有可能性?
- python - Python:插值 x、y、z 数据
- amazon-web-services - 加载 s3 时出现 java.lang.NoSuchMethodError
- kubernetes - 按环境删除所有 k8s 对象