无法在 google 地图中添加多个标记(抖动)
Cannot add more than one marker in google map (flutter)
我正在尝试在 google 地图中添加多个标记,但是一旦我调用该函数,只显示最后一个标记。我错过了什么?
我读到 我必须传递一个动态 markerId,我想我已经在做,但由于某种原因我无法获得第一个标记,总是最后一个。
注意:这不是拼写错误类型的问题 - 如果我删除 last
标记,则会显示 first
标记。所以它一定与用 last
覆盖 first
标记的函数有关:
void Markers() async {
var firstPath = "img/first.png";
final Uint8List firstIcon = await getBytesFromAsset(firstPath, 50);
var lastPath = "img/last.png";
final Uint8List lastIcon = await getBytesFromAsset(lastPath, 50);
_listmarkers = Set<Marker>();
_listmarkers.add(Marker(
markerId: MarkerId(_navigationList.first.userId.toString()),
position: LatLng(_navigationList.first.latitude,
_navigationList.first.longitude),
icon: BitmapDescriptor.fromBytes(firstIcon),
));
_listmarkers.add(Marker(
markerId: MarkerId(_navigationList.last.userId.toString()),
position: LatLng(_navigationList.last.latitude,
_navigationList.last.longitude),
icon: BitmapDescriptor.fromBytes(lastIcon),
));
setState(() {});
}
void createMap() {
if (_navigationList.isNotEmpty) {
_postsController.add(1);
LatLngBounds bound = boundsFromLatLngList(_navigationList);
Markers();
}
}
很有可能
_navigationList.last.userId.toString() == _navigationList.first.userId.toString()
我正在尝试在 google 地图中添加多个标记,但是一旦我调用该函数,只显示最后一个标记。我错过了什么?
我读到
注意:这不是拼写错误类型的问题 - 如果我删除 last
标记,则会显示 first
标记。所以它一定与用 last
覆盖 first
标记的函数有关:
void Markers() async {
var firstPath = "img/first.png";
final Uint8List firstIcon = await getBytesFromAsset(firstPath, 50);
var lastPath = "img/last.png";
final Uint8List lastIcon = await getBytesFromAsset(lastPath, 50);
_listmarkers = Set<Marker>();
_listmarkers.add(Marker(
markerId: MarkerId(_navigationList.first.userId.toString()),
position: LatLng(_navigationList.first.latitude,
_navigationList.first.longitude),
icon: BitmapDescriptor.fromBytes(firstIcon),
));
_listmarkers.add(Marker(
markerId: MarkerId(_navigationList.last.userId.toString()),
position: LatLng(_navigationList.last.latitude,
_navigationList.last.longitude),
icon: BitmapDescriptor.fromBytes(lastIcon),
));
setState(() {});
}
void createMap() {
if (_navigationList.isNotEmpty) {
_postsController.add(1);
LatLngBounds bound = boundsFromLatLngList(_navigationList);
Markers();
}
}
很有可能
_navigationList.last.userId.toString() == _navigationList.first.userId.toString()