迭代标记Google FlutterMap

vuktfyat  于 2022-12-05  发布在  Flutter
关注(0)|答案(4)|浏览(110)

我试图在新的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循环中的坐标,但它不起作用。
对空值调用了方法“addMarker”。
所以问题是,我如何使用google flutterMap包动态添加多个标记?
此外,我尝试了这个简单的代码和工程,所以错误是发生在添加标记。

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
无此类方法错误:对空值调用了getter“className.”

ghhkc1vu

ghhkc1vu1#

我在主构建函数中调用了Google maps的一个示例,如下所示:

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

这对我有用

xlpyo6sf

xlpyo6sf2#

在第一个例子中,GoogleMapController参数和你的局部变量有相同的名字,所以基本上,局部变量被遮蔽了,你给它自己赋函数参数值。重命名这两个变量中的一个应该可以解决这个问题。

zphenhs4

zphenhs43#

void populateOrder(Item item) async {
if (item != null) {
  for (int i = 0; i < item.feature.length; i++) {
    var position = item.feature[i];
    if (position != null) {
      try {
        if (double.parse(position.latitude) != null &&
            double.parse(position.longitude) != null) {
           mapController.clearMarkers().then((value){
             mapController.addMarker(
              MarkerOptions(
                position: LatLng(double.parse(position.latitude),
                    double.parse(position.longitude)),
                infoWindowText: InfoWindowText(position.schet, ""),
                icon: BitmapDescriptor.defaultMarker,
              ),
            );
          });
        }
      } catch (e) {
        print(e);
      }
    }
  }
}

}

void _onMapCreated(GoogleMapController controller) {
mapController = controller;
populateOrder(itemize);
Future.delayed(Duration(seconds: 6));

}

qc6wkl3g

qc6wkl3g4#

库已更新:这就是我在Map上显示多个标记的方法。

Completer<GoogleMapController> _controller = Completer();
         // initial camera position
         CameraPosition _cameraPos;
     // A map of markers
         Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
        // function to generate random ids
        int generateIds() {
              var rng = new Random();
              var randomInt;      
                randomInt = rng.nextInt(100);
                print(rng.nextInt(100));
              return randomInt;
            }

            //call this function in initstate
            // I'm getting a json Object with locations from an 
            // external api
            buildMarkers() {
                  for (var i = 0; i < gridItem.locations.length; i++) {
                    var markerIdVal = generateIds();
                  final MarkerId markerId = MarkerId(markerIdVal.toString());
                    final Marker marker = Marker(
                  markerId: markerId,
                  position: LatLng(
                   gridItem.locations[i].latitude,
                    gridItem.locations[i].longitude,
                  ),
                  infoWindow: InfoWindow(title: gridItem.locations[i].place, snippet: gridItem.locations[i].region),
                );
// you could do setState here when adding the markers to the Map
                 markers[markerId] = marker;

                  }
                  print("Length:: + ${markers.length}");
                }
    // your Googlemaps Widget somewhere in your widget tree
    GoogleMap(
              mapType: MapType.normal,
              initialCameraPosition: _cameraPos,
              gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[Factory<OneSequenceGestureRecognizer>(()=>ScaleGestureRecognizer())].toSet(),
              markers: Set<Marker>.of(markers.values),
              onMapCreated: (GoogleMapController controller) {
                             _controller.complete(controller);}),

相关问题