Flutter 谷歌地图:更新标记

Kar*_*dts 1 google-maps dart flutter

我正在尝试更新标记的坐标(Google 地图颤动)

我正在像这样“创建”标记。

 Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

Uint8List resizedMarkerImageBytesTemp = await convertUserImage(
        "imgurl");

    MarkerId id = MarkerId(uid);
    Marker _marker = Marker(
      markerId: id,
      onTap: () {
        print("tapped");
      },
      position: LatLng(lat, lng),
      icon: BitmapDescriptor.fromBytes(resizedMarkerImageBytesTemp),
      infoWindow: InfoWindow(title: 'myTitle'),
    );
    setState(() {
      markers[id] = _marker;
    });
Run Code Online (Sandbox Code Playgroud)

更新标记坐标的最佳/最有效方法是什么?

谢谢!

更新:我尝试了以下操作。

MarkerId id = MarkerId(uid); //uid of the marker I want to update

 markers[id].position.longitude = 3.12493;
Run Code Online (Sandbox Code Playgroud)

但后来我收到错误:

error: 'longitude' can't be used as a setter because it is final.
Run Code Online (Sandbox Code Playgroud)

Ita*_*cia 7

您可以创建一个函数并传递markerId来替换实际的标记:

   updateMarker(id){
 
  final marker = markers.values.toList().firstWhere((item) => item.markerId == id);

  Marker _marker = Marker(
   markerId: marker.markerId,
   onTap: () {
     print("tapped");
   },
   position: LatLng(marker.position.latitude, marker.position.longitude),
   icon: marker.icon,
   infoWindow: InfoWindow(title: 'my new Title'),
  );

 setState(() {
  //the marker is identified by the markerId and not with the index of the list
   markers[id] = _marker;
 });
}
Run Code Online (Sandbox Code Playgroud)