Flutter-如何删除单个google_maps_flutter ^ 0.5.21标记?

时间:2019-09-13 10:54:00

标签: ios list flutter dart google-maps-markers

google_maps_flutter诞生以来,情况已有很大变化,这意味着删除单个标记的过程也发生了变化。

我在较早的查询removing markers on version ~ 0.0.1上发现了以下问题:

mapController.markers.forEach((marker){
  mapController.removeMarker(marker);
});

这对我不起作用,因为出现错误:

The getter 'markers' isn't defined for the class 'GoogleMapController'.

The method 'removeMarker' isn't defined for the class 'GoogleMapController'.

这是我用来添加标记的方法:

void _addMarker(LatLng latlang, String title) {

var _width = MediaQuery.of(context).size.width;
var _height = MediaQuery.of(context).size.height;

double _topHeight = 55;

if (_height == 896.0 && _width == 414.0) {
  _topHeight = 83;
} else if (_height == 812.0 && _width == 375.0) {
  _topHeight = 78;
}

double mapsettingsHeight = 225;

if (_topHeight == 55){
  mapsettingsHeight = 225;
} else {
  mapsettingsHeight = 255;
}

if (_markers.contains(title)) {
  print("error");
} else {
  setState(() {
    _markers.add(Marker(
      markerId: MarkerId(title),
      position: latlang,
      infoWindow: InfoWindow(
        title: title,
        snippet: title,
        onTap: () {
          showCupertinoModalPopup(
            context: context,
            builder: (BuildContext context) {
              return Material(
                child: Container(
                  width: _width,
                  height: mapsettingsHeight,
                  decoration: BoxDecoration(
                    color: Color(0xFFF9F9F9).withAlpha(200),
                    borderRadius: BorderRadius.only(topLeft:Radius.circular(10),topRight:Radius.circular(10))
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: _width,
                        height: 40,
                        padding: EdgeInsets.all(10),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              height: 20,
                              alignment: Alignment.center,
                              child: Text(title,style:TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.black,fontWeight:FontWeight.w600))
                            ),
                            GestureDetector(
                              child: Container(
                                width: 50,
                                height: 50,
                                alignment: Alignment.topRight,
                                child: Icon(Icons.keyboard_arrow_down,size:25,color:Colors.black.withAlpha(100)),
                              ),
                              onTap: () {
                                Navigator.pop(context);
                              }
                            )
                          ],
                        ),
                      ),
                      _customDivider(),
                      Divider(height:10,color:Colors.transparent),
                      Container(
                        width: _width,
                        height: 12.5,
                        padding: EdgeInsets.only(left:10,right:10),
                        child: GestureDetector(
                          child: Text("Edit Marker",style: TextStyle(fontFamily:'Helvetica',fontSize:12.5,color:Colors.blue,fontWeight:FontWeight.w400)),
                          onTap: () {
                            showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return AlertDialog(
                                  title: TextField(
                                    controller: _locationMarkerTitle,
                                    decoration: InputDecoration(
                                      hintText: 'Name this location',
                                      hintStyle: TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.black,fontWeight:FontWeight.w200),
                                    ),
                                  ),
                                  titlePadding: EdgeInsets.all(10),
                                  content: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: <Widget>[
                                      GestureDetector(
                                        child: Text("Submit (this doesn't work!)",style: TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.blue,fontWeight:FontWeight.w200)),
                                        onTap: () {
                                          _addMarker(LatLng(_position.latitude,_position.longitude),_locationMarkerTitle.text);
                                          Navigator.pop(context);
                                        }
                                      ),
                                    ],
                                  )
                                );
                              }
                            );
                          }
                        ),
                      ),
                      Divider(height:10,color:Colors.transparent),
                      Container(
                        width: _width,
                        height: 12.5,
                        padding: EdgeInsets.only(left:10,right:10),
                        child: GestureDetector(
                          child: Text("Remove Marker",style: TextStyle(fontFamily:'Helvetica',fontSize:12.5,color:Colors.blue,fontWeight:FontWeight.w400)),
                          onTap: () {
                            setState(() {
                              //where the solution go
                            });
                          }
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }
          );
        }
      ),
      icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueAzure),
    ));
  });
}
}

1 个答案:

答案 0 :(得分:2)

我假设您的build()函数中某处有Google Maps小部件:

GoogleMap(
  markers: _markers,
  ...

您只需要从Marker列表中删除单个_markers,然后调用setState,以便使用更新的标记列表重建GoogleMap小部件。

编辑-更具体地针对作者的用例:

MarkerIds唯一标识一个标记,因此您可以使用这些标记来查找要删除的标记:

_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId.value == title));

_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId == MarkerId(title)));