删除google_maps_flutter中的标记

问题描述 投票:2回答:3

我添加了Google Maps for Flutter我知道如何添加标记,因为它在这些例子中清楚地给出了

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.
google-maps flutter google-maps-markers
3个回答
2
投票

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


2
投票

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

mapController.clearMarkers();

另一个是通过针对mapController.markers返回的每个标记

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

0
投票

我自己用google_maps_library遇到了这个问题,这个问题的主要原因'_markers[marker._id] == marker': is not true.是所有GoogleMapsController方法返回Future的事实,所以这个错误是,让我们说一个并发问题,因为方法cals是async

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

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

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

因此,如果使用标记添加/删除/更新执行任何操作,则应确保完成涉及标记的先前操作。

© www.soinside.com 2019 - 2024. All rights reserved.