Google maps 删除谷歌地图中的标记

Google maps 删除谷歌地图中的标记,google-maps,flutter,google-maps-markers,Google Maps,Flutter,Google Maps Markers,我补充说 我知道如何添加标记,因为在这些示例中已经清楚地给出了标记 MarkerOptions _options = new MarkerOptions( position: LatLng( driver_lat, driver_lng, ), infoWindowText: const InfoWindowText('An interesting locati

我补充说 我知道如何添加标记,因为在这些示例中已经清楚地给出了标记

MarkerOptions _options = new MarkerOptions(
          position: LatLng(
            driver_lat,
            driver_lng,
          ),
          infoWindowText:
              const InfoWindowText('An interesting location', '*'));

      Marker marker = new Marker('1', _options);

      //Adding Marker
      googleMapController.addMarker(_options);
我正在移除标记,如下所示

googleMapController.removeMarker(marker);
对于添加标记,它将MarkerOptions对象作为参数,但是对于删除标记,它将要求marker对象作为参数,并且我的删除标记代码不起作用。 我得到下面的错误

Failed assertion: line 201 pos 12: '_markers[marker._id] == marker': is not true.

使用
clearMarkers()
。它将清除地图中的所有标记。所以试试
googleMapController.clearMarkers()

我自己在
谷歌地图库
中遇到了这个问题,这个问题的主要原因是
''u markers[marker.\u id]==marker':不是真的。
是因为所有
谷歌地图控件
方法都返回一个
未来
,所以这个错误是,假设存在并发问题,因为方法CAL是
async

添加/删除标记的正确方法是:

_testRemoveMarker() async {
    Marker marker = await _mapController.addMarker(...markerOption..);
    _mapController.removeMarker(marker);
} 

_clearMarkersAndRead() async {
   _mapController.clearMarkers().then((_) {
       //TODO: add makrers as you whish;
   });
}

因此,如果您使用标记添加/删除/更新执行任何操作,您应该确保涉及标记的上一个操作已完成。

有两种方法可以做到这一点,一种是通过
clearMarkers()
方法

mapController.clearMarkers();
另一种是通过定位
mapController.markers返回的每个标记

mapController.markers.forEach((marker){
      mapController.removeMarker(marker);
});
答复:

.clearMarkers()已被弃用,因为现在每个标记都是存储在地图中的小部件。现在清除Google地图上所有标记的正确方法是将标记地图的状态设置为空地图

e、 g


你能分享你所有的错误行和你用来删除的整个代码块吗?你能确定那个标记在那个时刻不是空的吗?因为在这个例子中,当尝试这个
googleMapController.removeMarker(marker)时,这里的错误看起来非常简单v2中没有这样的方法
                ...
                  onPressed: () {
                    setState(() {
                      gMapMarkers = {};
                    });
                  }
                ....