[OpenLayers-Users] VectorLayer destroyFeatures doesn't work
properly?
gtg287y
gtg287y at gmail.com
Tue Apr 6 15:42:13 EDT 2010
I have a vectorLayer, polygonLayer, which I am calling
destroyFeatures(polygonLayer.features) on. I can confirm that after calling
it, the features go away in the polygonLayer.features variable. However, the
features (polygons drawn by the DrawFeatures controller as per the example
found at:
http://openlayers.org/dev/examples/regular-polygons.html
http://openlayers.org/dev/examples/regular-polygons.html
) don't actually go away on screen (they are still drawn on the map).
I thought maybe calling redraw() on the vectorLayer would help, but no dice.
Even though I have only one feature in the layers' array, I am seeing
several circles on screen.
>From the API, I got the impression that the features would be both erased
AND removed from the array. Was I wrong? How can I get the features to go
away?
Background: I only want one polygon on screen at a time, thus when the
event "beforeaddingfeature" is called, I clear all existing features, and
then let the current feature be added like normal.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code:
<html>
<head>
<h1> Select Blackout Area </h1>
<script type="text/javascript" src="javascripts/jquery-1.3.2.js"></script>
<script type="text/javascript"
src="javascripts/jquery.timers-1.2.js"></script>
<!-- bring in the OpenLayers javascript library
(here we bring it from the remote site, but you could
easily serve up this javascript yourself) -->
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script src="/javascripts/OpenStreetMap.js"></script>
<script type="text/javascript">
// making this a global variable so that it is accessible for
// debugging/inspecting in Firebug
var map, polygonControl, polygonLayer;
var bounds = new OpenLayers.Bounds(-125, 25, -65, 50);
function init(){
var navControl = new OpenLayers.Control.Navigation();
map = new OpenLayers.Map ("map", {
controls:[
navControl,
//new OpenLayers.Control.PanZoomBar(),
//new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Attribution()],
numZoomLevels: 10,
//restrictedExtent: bounds.clone().transform(EPSG4326, EPSG900913),
restrictedExtent: bounds.clone().transform(
new OpenLayers.Projection("EPSG:4326"),
new OpenLayers.Projection("EPSG:900913")),
maxExtent: bounds.clone().transform(
new OpenLayers.Projection("EPSG:4326"),
new OpenLayers.Projection("EPSG:900913")),
maxResolution: "auto",
units: 'm',
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
} );
//actually draws the map
layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik", {
displayOutsideMaxExtent: false,
wrapDateLine: true
//numZoomLevels: 4
});
polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
map.addLayer(layerMapnik);
map.addLayer(polygonLayer);
map.addControl(new OpenLayers.Control.MousePosition());
map.addControl(new OpenLayers.Control.LayerSwitcher());
var polyOptions = {sides: 40};
polygonControl = new
OpenLayers.Control.DrawFeature(polygonLayer,
OpenLayers.Handler.RegularPolygon,
{handlerOptions: polyOptions});
map.setCenter(new OpenLayers.LonLat(0, 0), 3);
//polygonControl.activate();
map.addControl(polygonControl);
map.zoomToMaxExtent();
var feature;
polygonLayer.events.register("beforefeatureadded", feature,
function(evt){
//only one circle at a time
clearCircles();
});//end attempt at events registration
}//end init
function setSize(fraction) {
var radius = fraction * map.getExtent().getHeight();
polygonControl.handler.setOptions({radius: radius,
angle: 0});
}//end setSize
function clearCircles(){
console.log("Clearing existing circles");
console.log(polygonLayer.features.length);
polygonLayer.destroyFeatures(polygonLayer.features);
polygonLayer.redraw();
}//end clearCircles
</script>
</script>
</head>
<body onload="init()">
<ul id="controls">Map Controls
<input type="radio" name="type"
value="none" id="noneToggle"
onclick="polygonControl.deactivate()"
checked="checked" />
<label for="noneToggle">navigate</label>
<input type="radio" name="type"
value="polygon" id="polygonToggle"
onclick="polygonControl.activate()" />
<label for="polygonToggle">draw polygon</label>
</ul>
<div id="map" class="small_map"></div>
</body>
</html>
--
View this message in context: http://n2.nabble.com/VectorLayer-destroyFeatures-doesn-t-work-properly-tp4860918p4860918.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
More information about the Users
mailing list