admin管理员组

文章数量:1405316

would really appreciate any and all help on the following issue:

I am using openlayers 3 to draw polygons. What I want to achieve is as follows - when starting to draw a new polygon - remove any existing ones from the map so only a single polygon could be drawn at any one time. However, what happens is that the feature (polygon) is only removed from the memory but still stays visible on the map.

This jsfiddle demonstrates the problem: /

As you can see, onDrawStart I am first clearing tempVectorSource (where the polygons are drawn, since featureOverlay.source===tempVectorSource) and then alerting the number of features for the tempVectorSource. The alert will always indicate 0, as expected, but the polygons are still visible on the map.

function onDrawStart(event)
{
     //remove everything drawn previously
     tempVectorSource.clear();

     //can see the feature(s) are removed from memory
     alert(tempVectorSource.getFeatures().length);
}

Thanks in advance!

EDIT: Found a workaround for anyone who encounters the same issue - set all of the previously drawn features' geometries to a point with no coords before calling clear on the vector source, as follows:

function onDrawStart(event)
{
    var features = tempVectorSource.getFeatures();
    for(var i=0;i<features.length;i++)
    {
        features[i].setGeometry(new ol.geom.Point([]));
    }
    tempVectorSource.clear();

}

The above essentially renders all the polygons into "invisible" points. Still curious to find a real solution though!

EDIT (2): Please see my ment below the accepted answer, I have featureOverlay set to an instance of a map where the shapes reside.. removing the following line fixes the issue since featureOverlay has its own copy of the shapes:

featureOverlay.setMap(map);

would really appreciate any and all help on the following issue:

I am using openlayers 3 to draw polygons. What I want to achieve is as follows - when starting to draw a new polygon - remove any existing ones from the map so only a single polygon could be drawn at any one time. However, what happens is that the feature (polygon) is only removed from the memory but still stays visible on the map.

This jsfiddle demonstrates the problem: http://jsfiddle/jp4dojwu/

As you can see, onDrawStart I am first clearing tempVectorSource (where the polygons are drawn, since featureOverlay.source===tempVectorSource) and then alerting the number of features for the tempVectorSource. The alert will always indicate 0, as expected, but the polygons are still visible on the map.

function onDrawStart(event)
{
     //remove everything drawn previously
     tempVectorSource.clear();

     //can see the feature(s) are removed from memory
     alert(tempVectorSource.getFeatures().length);
}

Thanks in advance!

EDIT: Found a workaround for anyone who encounters the same issue - set all of the previously drawn features' geometries to a point with no coords before calling clear on the vector source, as follows:

function onDrawStart(event)
{
    var features = tempVectorSource.getFeatures();
    for(var i=0;i<features.length;i++)
    {
        features[i].setGeometry(new ol.geom.Point([]));
    }
    tempVectorSource.clear();

}

The above essentially renders all the polygons into "invisible" points. Still curious to find a real solution though!

EDIT (2): Please see my ment below the accepted answer, I have featureOverlay set to an instance of a map where the shapes reside.. removing the following line fixes the issue since featureOverlay has its own copy of the shapes:

featureOverlay.setMap(map);
Share Improve this question edited Nov 20, 2014 at 12:26 MS86 asked Sep 26, 2014 at 9:31 MS86MS86 431 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

Don't know if that helps, but I had a related problem:

I wrote that piece of code, that works just fine to "reset" the drawing layer. You could trigger that function with onDrawStart first and after that just start the drawing part.

function clearMap() {
  vector_layer.getSource().clear();
  if (select_interaction) {
    select_interaction.getFeatures().clear();
  }
}

This works with the latest ol3-build (EDIT: which was v3.0.0 at that time)

本文标签: javascriptOpenLayers 3 removing features does not remove the feature from the mapStack Overflow