【发布时间】:2019-10-06 20:04:12
【问题描述】:
我正在尝试显示当前位置的地图。为此,我使用了 Location 和 Google Map 插件(最新版本)。
我有一个类似的代码:
var lng, lat;
@override
initState() {
super.initState();
loading = true;
getLocation();
}
Future getLocation() async {
final location = Location();
var currentLocation = await location.getLocation();
setState(() {
lat = currentLocation.latitude;
lng = currentLocation.longitude;
loading=false;
});
}
loading==false ? GoogleMap(
mapType: MapType.hybrid,
myLocationButtonEnabled: true,
myLocationEnabled: true,
initialCameraPosition: CameraPosition(
target: LatLng(lat, lng),
zoom: 15.0,
)):null,
当我们使用地图查看视图时,会在 1 秒左右出现错误(然后视图加载正确),并在控制台中显示此错误
I/flutter (15567):══╡ 小部件库发现异常 ╞═════════════════════════════════════════════════ ══════════ I/颤振 (15567):以下断言被抛出构建 HomePageScreen(脏,依赖项:I/flutter(15567): [_LocalizationsScope-[GlobalKey#78c30],MediaQuery],状态: _HomePageScreen#9c9d2): I/flutter (15567): 'package:google_maps_flutter/src/location.dart': 断言失败: 第 17 行 pos 16: '纬度!= I/flutter (15567): null': 不正确。
我调试了一下,错误比较简单:Location插件加载经纬度慢,Google Maps插件加载更快。所以我们有一个错误。
问题是:如何强制谷歌地图等待位置插件的位置和经度?
【问题讨论】:
-
定义等待:是隐藏地图还是有默认点?
-
我想在加载当前位置时隐藏它。默认点是当前位置(借助 Location 插件)
标签: google-maps flutter