gpt4 book ai didi

google-maps - flutter 谷歌地图: update markers

转载 作者:行者123 更新时间:2023-12-03 04:26:29 24 4
gpt4 key购买 nike

我正在尝试更新标记的坐标(谷歌地图 flutter )

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

 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;
});

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

谢谢!

更新:我尝试了以下。
MarkerId id = MarkerId(uid); //uid of the marker I want to update

markers[id].position.longitude = 3.12493;

但后来我得到了错误:
error: 'longitude' can't be used as a setter because it is final.

最佳答案

您可以创建一个函数并传递 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;
});
}

关于google-maps - flutter 谷歌地图: update markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59468932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com