【发布时间】:2021-09-08 01:51:16
【问题描述】:
我有一个带有 GoogleMap 的简单 Flutter 应用程序。 我想要做的是,当单击按钮时,我的数组中的所有标记都会显示在地图上。
注意 - 我能够从 Google API 收集和存储所有与地点相关的信息,并且可以使用它。数据不是问题。唯一的问题是显示我的数组中的标记。
我有 2 组代码,(1) GoogleMap 集成和 (2) 用于创建标记并将每个标记推送到我的数组的代码。
(1)
Container(
child: GoogleMap(
markers: Set<Marker>.from(multipleMarkers),
polylines: polylines,
trafficEnabled: false,
rotateGesturesEnabled: true,
buildingsEnabled: true,
myLocationEnabled: true,
myLocationButtonEnabled: false,
mapType: MapType.normal,
zoomControlsEnabled: false,
initialCameraPosition: CameraPosition(target:currentLatLng, zoom: 15),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
setPolylines();
},
),
),
(2) 该方法链接到一个按钮。
addMultipleMarkers(LatLng latLng) {
//this method finds the total number of Lat-Lngs in the array -> 20 Lat-Lngs exist
print("'addMultipleMarkers()' ====================> ${nearbySearchResult.length}");
for (var i in nearbySearchResult){
double? lat = 0;
double? lng = 0;
if (i.geometry!.location!.lat != null){
lat = i.geometry!.location!.lat;
}
if (i.geometry!.location!.lng != null){
lng = i.geometry!.location!.lng;
}
//creating marker, adding info, and pushing it to 'multitpleMarkers' array
Marker marker = Marker(
markerId: MarkerId(i.geometry!.location.toString()),
position: LatLng(lat!, lng!),
infoWindow: InfoWindow(
title: 'Origin',
snippet: "originSnippet"
),
icon: BitmapDescriptor.defaultMarker,
);
//I tried to setState each time marker is added in array - not working
setState(() {
multipleMarkers.add(marker);
});
}
//this print line confirms that 20 marks have have been successfully stored in 'multipleMarkers' array
print("============> TOTAL MARKERS " + multipleMarkers.length.toString());
}
【问题讨论】:
标签: flutter google-maps dart flutter-layout google-maps-markers