0

我在 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),
              ),
            );
          },
        );
      },
    );
  }
}  

我将此谷歌地图呈现为父小部件的子级。我想在点击父小部件中的按钮时向此地图添加一个新标记。

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

有人可以帮忙吗?

4

1 回答 1

0
      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;
      });
    }
  }
于 2021-12-16T05:34:53.563 回答