【发布时间】:2020-12-24 23:54:00
【问题描述】:
我正在尝试构建一个加载 GoogleMap() 的应用,然后在获取用户纬度和经度后移动到该特定位置。
我想出了这个想法(下面的代码),但它只有在我重新启动应用程序时才有效,如果我不这样做,它会给出错误:animateCamera 在 null 上被调用。
这怎么可能?我该如何解决?
感谢您的回答:D
...
var mapController;
...
GoogleMap createMap() {
var initMap = GoogleMap(
onMapCreated: onMapCreated,
initialCameraPosition:
CameraPosition(target: LatLng(47.290542, 8.322641), zoom: 6.7),
);
return initMap;
}
...
void onMapCreated(controller) {
mapController = controller;
}
...
void moveCameraToUserLocation(searchedLocation2) async {
var location = await Geocode().getLatLng(searchedLocation2);
print("moving to: $location");
mapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: location,
zoom: 20,
),
),
);
...
build(context) {
return Scaffold(
body: createMap(),
...
【问题讨论】:
标签: google-maps flutter google-maps-api-3