Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 颤振:如何设置谷歌地图相机的动画_Google Maps_Flutter_Google Maps Api 3 - Fatal编程技术网

Google maps 颤振:如何设置谷歌地图相机的动画

Google maps 颤振:如何设置谷歌地图相机的动画,google-maps,flutter,google-maps-api-3,Google Maps,Flutter,Google Maps Api 3,我正在尝试构建一个加载GoogleMap()的应用程序,然后在获得用户纬度和经度后,移动到该特定位置 我提出了这个想法(下面的代码),但它只有在我重新启动应用程序时才起作用,如果我不重启,它会给出错误:animateCamera被调用为null 怎么可能呢?我怎样才能修好它 谢谢回答:D 。。。 var映射控制器; ... GoogleMap createMap(){ var initMap=GoogleMap( onMapCreated:onMapCreated, 初始摄像机位置: 摄像机位

我正在尝试构建一个加载GoogleMap()的应用程序,然后在获得用户纬度和经度后,移动到该特定位置

我提出了这个想法(下面的代码),但它只有在我重新启动应用程序时才起作用,如果我不重启,它会给出错误:animateCamera被调用为null

怎么可能呢?我怎样才能修好它

谢谢回答:D

。。。
var映射控制器;
...
GoogleMap createMap(){
var initMap=GoogleMap(
onMapCreated:onMapCreated,
初始摄像机位置:
摄像机位置(目标:LatLng(47.290542,8.322641),变焦:6.7),
);
返回initMap;
}
...
已创建无效(控制器){
mapController=控制器;
}
...
void moveCameraToUserLocation(searchedLocation2)异步{
var location=await Geocode().getLatLng(searchedLocation2);
打印(“移动到:$location”);
mapController.animateCamera(
CameraUpdate.newCameraPosition(
摄像定位(
目标:地点,
缩放:20,
),
),
);
...
构建(上下文){
返回脚手架(
正文:createMap(),

在创建映射之前,您可能正在调用moveCameraToUserLocation函数。从这部分代码中,当您调用此函数时,我无法使用它,但我猜您是从initstate调用它的。如果要在创建小部件后立即调用此函数,请将其移动到onMapCreated

void onMapCreated(controller) {
   mapController = controller;
   moveCameraToUserLocation();
}

根据您对问题的描述,看起来您只是在加载贴图时调用animate函数


我建议您创建一个按钮来处理和触发动画功能,并在点击时将相机移动到设备位置

下面的代码段使用了一个
ClipOval
按钮,在
OnTap()
事件中,您可以调用该方法来获取设备当前位置,只要点击按钮,就会触发该位置

ClipOval(
   child: Material(
   color: Colors.green[100], // button color
   child: InkWell(
   splashColor: Colors.green, // inkwell color
   child: SizedBox(
   width: 56,
   height: 56,
   child: Icon(Icons.my_location),
   ),
   onTap: () {

    // Add methods here that will be called whenever the button is tapped 

    mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
    target: LatLng(_currentPosition.latitude, _currentPosition.longitude),
    zoom: 17.0)));
    setState(() {
    allMarkers.add(Marker(
      markerId: MarkerId('Current Position'),
      draggable: false,
      position:
          LatLng(_currentPosition.latitude, _currentPosition.longitude)));
        });
      },
    ),
  ),
)