Ude*_*hUK 0 google-maps marker dart flutter
我想使用 Google Google Maps for Flutter和它的标记在 Flutter 中创建一个位置选择器。可以通过将可拖动选项设置为 true 来创建标记来创建可拖动标记。使用以下代码段。
Widget _buildMap(BuildContext context) {
return GoogleMap(
options: GoogleMapOptions(
cameraPosition: CameraPosition(
target: LatLng(7.2906, 80.6337),
zoom: 7.0,
),
compassEnabled: true,
),
onMapCreated: (controller) {
_mapController = controller;
controller.addMarker(
MarkerOptions(
draggable: true,
flat: false,
position: LatLng(7.2906, 80.6337),
),
);
},
);
}
Run Code Online (Sandbox Code Playgroud)
但是在拖动标记后我找不到获取标记新位置的方法。我试图通过引用 的markers
属性来获取标记的新位置,MapController
但它返回标记的初始位置。
_mapController.markers.forEach((marker) {
print("Pos: " + marker.options.position.toString())
});
// Prints "Pos: LatLng[7.2906, 80.63369999999998]"
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么还是有另一种方法来完成这个用例?谢谢你。
我解决这个问题的方法是使用相机位置来移动标记,然后使用当前相机位置来获取新坐标。您需要稍微重构一下代码以使用 0.4 版的 google maps for flutter 中的最新更改,其中包括您需要添加到代码中的回调:
onCameraMove: ((_position) => _updateMarker(_position)),
Run Code Online (Sandbox Code Playgroud)
然后,您可以在每次用户移动相机时设置标记的新状态,并将此坐标用于您需要的任何其他目的:
void _updatePosition(CameraPosition _position) {
Position newMarkerPosition = Position(
latitude: _position.target.latitude,
longitude: _position.target.longitude);
Marker marker = markers["your_marker"];
setState(() {
markers["your_marker"] = marker.copyWith(
positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
});
}
Run Code Online (Sandbox Code Playgroud)
告诉我它是否有效!
归档时间: |
|
查看次数: |
6933 次 |
最近记录: |