如何在flutter中点击父窗口小部件上的按钮,将标记添加到谷歌地图中



我在Flutter中为谷歌地图提供了以下类。

class ExchangeHousesMap extends StatefulWidget {
final List<Marker> markers;
final double initialLatitude;
final double initialLongitude;
const ExchangeHousesMap({
Key? key,
required this.initialLatitude,
required this.initialLongitude,
required this.markers,
}) : super(key: key);
@override
State<ExchangeHousesMap> createState() => ExchangeHousesMapState();
}
class ExchangeHousesMapState extends State<ExchangeHousesMap> {
late final CameraPosition _initialCameraPosition;
late final Set<Marker> _markers = {};
final Completer<GoogleMapController> _controller = Completer();
@override
void initState() {
super.initState();
_initialCameraPosition = CameraPosition(
target: LatLng(widget.initialLatitude, widget.initialLongitude),
zoom: 12,
);
}
@override
Widget build(BuildContext context) {
return GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _initialCameraPosition,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
setState(
() {
_markers.addAll(widget.markers);
_markers.add(
Marker(
markerId: const MarkerId('user-marker'),
position: LatLng(widget.initialLatitude, widget.initialLongitude),
),
);
},
);
},
);
}
}  

我将此谷歌地图渲染为父小部件的子级。我想在点击父窗口小部件中的按钮后,向该映射添加一个新标记

我不知道该怎么做。如何在点击父窗口小部件中的按钮时将标记添加到此地图?

有人能帮忙吗?

GoogleMap(
initialCameraPosition: _initialCameraPosition,
onMapCreated: (controller) => _googleMapController = controller,
zoomControlsEnabled: false,
myLocationButtonEnabled: false,
markers: {
if (_origin != null) _origin!,
if (_destination != null) _destination!,
},
onLongPress: _addMarker,  //for adding marker on long press
),

用于在长按上添加标记

void _addMarker(LatLng pos) async {
if (_origin == null || (_origin != null && _destination != null)) {
setState(() {
_origin = Marker(
markerId: const MarkerId("origin"),
infoWindow: const InfoWindow(title: "Origin"),
icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueGreen),
position: pos);
//Reset destination and info
_destination = null;
_info = null;
});
} else {
setState(() {
_destination = Marker(
markerId: const MarkerId("destination"),
infoWindow: const InfoWindow(title: "Destination"),
icon:
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
position: pos);
});
//Get directions
final directions = await DirectionsRepository()
.getDirections(origin: _origin!.position, destination: pos);
setState(() {
_info = directions;
});
}
}

相关内容

  • 没有找到相关文章

最新更新