我正在尝试为谷歌地图创建多个标记。我从Set<Marker> markers = Set();
开始,然后在List上循环,并将Markers添加到markers
中
Marker resultMarker = Marker(
markerId: MarkerId(currentLocation.id),
infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
);
markers.add(resultMarker);
然后我试着返回一个小部件:
GoogleMap(
initialCameraPosition: CameraPosition(target: _cameraPosition, zoom: 11.0),
onMapCreated: (controller) => mapController = controller,
myLocationButtonEnabled: false,
markers: markers,
);
但我只得到了第一个标记,而不是所有的标记。如何获取所有标记?
这个对我有用:
List<Marker> markers = <Marker>[];
在你的循环中:
markers.add(
Marker(
markerId: MarkerId(currentLocation.id),
infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
)
);
确保标记数据是正确的,因为谷歌地图没有显示具有无效LatLng
的标记(尝试使用伪数据进行测试(
然后
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(38.9647,35.2233),
zoom: 9.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
)