Flutter-Google Maps等不及去位置 [英] Flutter - Google Maps doesn´t wait to Location
问题描述
我正在尝试显示当前位置的地图.为此,我使用了 位置 和 Google Map 插件(最新版本).
I´m trying to show a map with current location. For do this i used Location and Google Map plugins (last version).
我有类似的代码:
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秒(然后正确加载了视图)
When we go the view with map a error appear around 1s (then view load properly) with this error in the console
I/flutter(15567):══╡小组件库引起的异常提示 ╞═════════════════════════════════════════════════ ══════════我/扑 (15567):以下断言被抛出 HomePageScreen(脏,依赖项:I/flutter(15567): [_LocalizationsScope- [GlobalKey#78c30],MediaQuery],状态: _HomePageScreen#9c9d2):I/flutter(15567):"package:google_maps_flutter/src/location.dart":失败的断言: 第17行pos 16:'latitude!= I/flutter(15567):null':不正确.
I/flutter (15567): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (15567): The following assertion was thrown building HomePageScreen(dirty, dependencies: I/flutter (15567): [_LocalizationsScope-[GlobalKey#78c30], MediaQuery], state: _HomePageScreen#9c9d2): I/flutter (15567): 'package:google_maps_flutter/src/location.dart': Failed assertion: line 17 pos 16: 'latitude != I/flutter (15567): null': is not true.
我对其进行调试,错误相对简单: Location 插件加载纬度和经度较慢, Google Maps 插件加载速度更快.所以我们有一个错误.
I debug it and the error is relative simple: The Location plugin load latitude and longitude slow , Google Maps plugin load more fast. So we have a error.
问题是:如何强制Google地图通过位置"插件等待位置和经度?
The question is: How can i force Google map to wait location and longitude from Location plugin?
推荐答案
在lat
和lng
为空时显示空的Container()
或任何加载指示器.
Show an empty Container()
or any loading indicator while your lat
and lng
is null.
lat == null || lng == null
? Container()
: GoogleMap(
mapType: MapType.hybrid,
myLocationButtonEnabled: true,
myLocationEnabled: true,
initialCameraPosition: CameraPosition(
target: LatLng(lat, lng),
zoom: 15.0,
),
);
这篇关于Flutter-Google Maps等不及去位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!