首页 > 解决方案 > Flutter - AnimateCamera 无法与 newLatLngBounds 一起使用

问题描述

我正在使用谷歌地图,在添加了很多标记后,我想将相机移动到 newLatLngBounds 以显示用户可见的所有标记。但我正面临这个错误。

使用 newLatLngBounds(LatLngBounds, int) 时出错:地图大小不能为 0。很可能,地图视图的布局尚未发生。要么等到布局发生,要么使用 newLatLngBounds(LatLngBounds, int, int, int) 允许您指定地图的尺寸​​。, null)

Future<void> getCenterMap() async {
double minlatitude = loadInformationMap[0]['latlng'].latitude,
    maxlatitude = loadInformationMap[0]['latlng'].latitude,
    minlongitude = loadInformationMap[0]['latlng'].longitude,
    maxlongitude = loadInformationMap[0]['latlng'].longitude;
for (int i = 0; i < loadInformationMap.length; i++) {
  if (minlatitude >= loadInformationMap[i]['latlng'].latitude) {
    minlatitude = loadInformationMap[i]['latlng'].latitude;
  }
  if (minlongitude >= loadInformationMap[i]['latlng'].longitude) {
    minlongitude = loadInformationMap[i]['latlng'].longitude;
  }
  if (maxlatitude <= loadInformationMap[i]['latlng'].latitude) {
    maxlatitude = loadInformationMap[i]['latlng'].latitude;
  }
  if (maxlongitude <= loadInformationMap[i]['latlng'].longitude) {
    maxlongitude = loadInformationMap[i]['latlng'].longitude;
  }
}
googleMapController.animateCamera(CameraUpdate.newLatLngBounds(
    LatLngBounds(
        southwest: LatLng(minlatitude, minlongitude),
        northeast: LatLng(maxlatitude, maxlongitude)),
    100));
}

标签: google-mapsflutterdartgoogle-maps-markerslatitude-longitude

解决方案


推荐阅读