【问题标题】:How to automatically set Google Maps Viewport contain a list of Markers in Flutter?如何自动设置 Google Maps Viewport 包含 Flutter 中的标记列表?
【发布时间】:2021-07-06 09:35:18
【问题描述】:
我想在 Flutter 中使用标记列表自动将插件 google_maps_flutter 的 Google 地图居中。标记是从 API 返回的,我想用 CameraPosition 打开谷歌地图,其中包含所有带有一些填充的标记。我该怎么做?
我已经使用that 示例来集成地图。
【问题讨论】:
标签:
flutter
google-maps
geometry
【解决方案1】:
这是一个显示 2 个标记的示例。您可以从标记列表中获取西南和东北位置。
GoogleMap(
initialCameraPosition: _initPosition,
onMapCreated: (GoogleMapController controller) {
//Camera Update to include 2 marker location
LatLngBounds bound;
if (loc1.lat > loc2.lat &&
loc1.lng > loc2.lng) {
bound = LatLngBounds(
southwest: LatLng(loc2.lat, loc2.lng),
northeast: LatLng(loc1.lat, loc1.lng),
);
} else if (loc1.lng > loc2.lng) {
bound = LatLngBounds(
southwest: LatLng(loc1.lat, loc2.lng),
northeast: LatLng(loc2.lat, loc1.lng));
} else if (loc1.lat > loc2.lat) {
bound = LatLngBounds(
southwest: LatLng(loc2.lat, loc1.lng),
northeast: LatLng(loc1.lat, loc2.lng));
} else {
bound = LatLngBounds(
southwest: LatLng(loc1.lat, loc1.lng),
northeast: LatLng(loc2.lat, loc2.lng),
);
}
CameraUpdate u2 =
CameraUpdate.newLatLngBounds(bound, 50);
Future.delayed(Duration(milliseconds: 500))
.then((value) => controller.animateCamera(u2));
},
)