我在 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),
),
);
},
);
},
);
}
}
我将此谷歌地图呈现为父小部件的子级。我想在点击父小部件中的按钮时向此地图添加一个新标记。
我不知道该怎么做。在父小部件中点击该按钮后,如何将标记添加到此地图?
有人可以帮忙吗?