我正在尝试在新的 google flutter map 中迭代标记.
我使用 Web 服务检索坐标数组,然后迭代元素并获取具有纬度和经度的索引。
for (int i = 0; i < list.length; i++) {
mapController.addMarker(MarkerOptions(position: list[0].values.elementAt(i)))));
}
还有 map 选项。
GoogleMapController mapController;
GoogleMap(
onMapCreated: (GoogleMapController mapController) {
mapController = mapController;
},
options: GoogleMapOptions(
mapType: MapType.satellite,
myLocationEnabled :true,
cameraPosition: CameraPosition(
target: LatLng(40.347022, -3.750381), zoom: 5.0),
),
),
我认为 mapController 应该采用我放在 for 循环中的坐标,但它不起作用。控制台返回
The method 'addMarker' was called on null.
所以问题是,如何使用谷歌 flutter map 包动态添加多个标记?
我也试过这个简单的代码并且工作正常,所以添加标记时出现错误。
GoogleMapController mapController;
GoogleMap(
onMapCreated: (GoogleMapController mapController) {
mapController = mapController;
mapController.addMarker(MarkerOptions(
position:LatLng(40.347022, -3.750381),
infoWindowText: InfoWindowText("Title", "Content"),
//icon:
));
mapController.addMarker(MarkerOptions(
position:LatLng(43.321871, -3.006887),
infoWindowText: InfoWindowText("Title", "Content"),
//icon:
));
},
options: GoogleMapOptions(
mapType: MapType.satellite,
myLocationEnabled :true,
cameraPosition: CameraPosition(
target: LatLng(40.347022, -3.750381), zoom: 5.0),
),
),
更新 2
我找到了这个示例代码。这正是我想要的,但我不能重复此代码,返回此错误
https://github.com/gerryhigh/Flutter-Google-Maps-Demo/blob/master/lib/venues.dart
NoSuchMethodError: The getter 'className' was called on null.
最佳答案
我在主构建函数中调用了一个谷歌地图的实例。像这样:
return GoogleMap(
onMapCreated: _onMapCreated,
options: GoogleMapOptions(
cameraPosition: CameraPosition(
target: _center1,
zoom: 11.0,
),
),
);
“_onMapCreated”函数内部是迭代器:
widget.model.allItems.forEach((item) {
//print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
print(item.lat);
var newItem = LatLng(double.parse(item.lat), double.parse(item.lon));
mapController.addMarker(
MarkerOptions(
icon: BitmapDescriptor.fromAsset(assetName),
position: newItem,
),
);
});
这对我有用。
关于google-maps - 迭代标记 Google Flutter map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074376/