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
1 Answer
Reset to default 4Don'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
版权声明:本文标题:javascript - OpenLayers 3 removing features does not remove the feature from the map - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744281216a2598659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论