如何在带有颤动的谷歌地图上长按放置图钉?

Shr*_*rma 0 google-maps dart flutter

我是扑的新手。我试图用颤动在地图上放置图钉,

在这里,我使用 geolocator 包获取了当前位置并设置了标记


 GoogleMap(
            onMapCreated: (controller){
              mapController=controller ;
            },
            mapType: _currentMapType,
            myLocationEnabled: true,
            initialCameraPosition: CameraPosition(
              target:_center,
              zoom: 11.0,
            ),
            markers: {
              //Marker for current Location
              Marker(
                markerId: MarkerId("marker"),
                position: LatLng(currentPosition.latitude, currentPosition.longitude),
                infoWindow: InfoWindow(title: 'Current Location'),
                icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed)
              )  
            },
          ),

Run Code Online (Sandbox Code Playgroud)

小智 5

我最近在研究这个代码,它可能对你有帮助。

1.-首先以这种方式定义标记(将其作为全局变量):

Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Run Code Online (Sandbox Code Playgroud)

2.- 创建谷歌地图小部件:

@override
Widget build(BuildContext context) {
    return new Scaffold(
        body: Stack(
            children: [Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: GoogleMap(
                    mapType: _defaultMapType,
                    myLocationEnabled: true,
                    myLocationButtonEnabled: true,
                    initialCameraPosition: _currentposition,
                    onMapCreated: (GoogleMapController controller) {
                      _controller.complete(controller);
                    },
                    compassEnabled: true,
                    tiltGesturesEnabled: false,
                    onLongPress: (latlang) {
                        _addMarkerLongPressed(latlang); //we will call this function when pressed on the map
                    },
                    markers: Set<Marker>.of(markers.values), //all markers are here
                )
            )]
        ),
    );
}
Run Code Online (Sandbox Code Playgroud)

3.- 创建函数(方法)'_addMarkerLongPressed':

Future _addMarkerLongPressed(LatLng latlang) async {
    setState(() {
        final MarkerId markerId = MarkerId("RANDOM_ID");
        Marker marker = Marker(
            markerId: markerId,
            draggable: true,
            position: latlang, //With this parameter you automatically obtain latitude and longitude
            infoWindow: InfoWindow(
                title: "Marker here",
                snippet: 'This looks good',
            ),
            icon: BitmapDescriptor.defaultMarker,
        );

        markers[markerId] = marker;
    });

    //This is optional, it will zoom when the marker has been created
    GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newLatLngZoom(latlang, 17.0));
}
Run Code Online (Sandbox Code Playgroud)

我希望我有所帮助:)