gpt4 book ai didi

google-maps - 用 flutter 的谷歌地图在屏幕上调整标记

转载 作者:行者123 更新时间:2023-12-03 23:50:50 25 4
gpt4 key购买 nike

我在玩Google Maps for Flutter .我希望 map View 适合屏幕上的所有标记。基本上,我想做与 here 相同的操作对于安卓。

我假设 cameraTargetBounds描述应该适合屏幕边界的区域。但实际上它并不完全适合这个领域,而是介于两者之间。

到目前为止我所做的:

  @override
Widget build(BuildContext context) {
final bloc = Provider.of<BlocMap>(context);
return SafeArea(
child: Container(
child: Stack(
children: <Widget>[
Positioned.fill(
child: StreamBuilder<MapData>(
stream: bloc.mapData,
builder: (context, snap) {
final mapData = snap.data;
print("mapData: $mapData");
return GoogleMap(
padding: EdgeInsets.only(bottom: 42),
mapType: _mapType,
cameraTargetBounds: mapData?.markers == null
? CameraTargetBounds.unbounded
: CameraTargetBounds(_bounds(mapData?.markers)),
initialCameraPosition: _cameraPosition,
myLocationEnabled: true,
myLocationButtonEnabled: true,
gestureRecognizers: _buildGestureRecognizer(),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
markers: mapData?.markers,
polylines: mapData?.polylines,
polygons: mapData?.polygons,
onLongPress: (latLng) {
print("LatLng: $latLng");
},
);
}),
),
Positioned(
left: 0,
right: 0,
bottom: 0,
child: _buildBtnBar(),
)
],
),
),
);

LatLngBounds _bounds(Set<Marker> markers) {
if (markers == null || markers.isEmpty) return null;
return _createBounds(markers.map((m) => m.position).toList());
}


LatLngBounds _createBounds(List<LatLng> positions) {
final southwestLat = positions.map((p) => p.latitude).reduce((value, element) => value < element ? value : element); // smallest
final southwestLon = positions.map((p) => p.longitude).reduce((value, element) => value < element ? value : element);
final northeastLat = positions.map((p) => p.latitude).reduce((value, element) => value > element ? value : element); // biggest
final northeastLon = positions.map((p) => p.longitude).reduce((value, element) => value > element ? value : element);
return LatLngBounds(
southwest: LatLng(southwestLat, southwestLon),
northeast: LatLng(northeastLat, northeastLon)
);
}

在 flutter 中实现这一基本功能的预期方法是什么?有什么建议么?

最佳答案

使用此代码做你想做的事。

onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);

setState(() {
controller.animateCamera(CameraUpdate.newLatLngBounds(_bounds(markers), 50));
});
}

关于google-maps - 用 flutter 的谷歌地图在屏幕上调整标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58094316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com