[OpenLayers-Users] removing a layer
Puneet Kishor
punk.kish at gmail.com
Sat Jan 21 16:51:16 EST 2012
anyone, any suggestions? I am stuck on this for a couple of three days now. Briefly, I want to remove all the features from a vector layer so I can draw them anew on every "zoomend"
Many thanks in advance.
On Jan 20, 2012, at 11:45 AM, Puneet Kishor wrote:
>
> On Jan 19, 2012, at 10:40 PM, Imran Rajjad wrote:
>
>> I think the correct function is
>>
>> layer.destroyFeatures();
>>
>> which will empty the vector layer. You should not remove or re-initialize
>> the layer. It makes sense to remove the layer from the map if you do not
>> need it further.
>
>
> Thanks, but nope, that does not work. here is my pseudocode triggered "onzoomend"
>
> var layer, features = [];
>
> function foo() {
> if (features.length > 0) {
> layer.destroyFeatures();
> features.length = 0;
> }
> else {
> layer = new OpenLayers.Layer.Vector("A vector layer");
> map.addLayer(layer);
> }
>
> // add features and store in array
> for (var i = 0; i < n; i++) {
> var feature = createFeature();
> features.push(feature);
> }
>
> layer.addFeatures(features);
> }
>
> Everytime I zoom in or out, the above is triggered. features[] has n elements all the time showing that it is emptied and refilled, but the layer from previous iteration is still visible on the map. Any other suggestions?
>
> This should not be so difficult but is confounding me. Maybe it needs its own special entry on a wiki or an FAQ.
>
>
>
>>
>> regards,
>> Imran
>> On Fri, Jan 20, 2012 at 3:19 AM, Puneet Kishor <punk.kish at gmail.com> wrote:
>>
>>> I am completely at my wits end. I am trying to check if a vector layer
>>> exists, and if it does, I need to either completely remove it or remove all
>>> the features from it (whichever is more efficient from the point of speed
>>> and memory), and then redraw that layer. Have been trying all sorts of
>>> permutations and combinations, but am just not successful.
>>>
>>> My latest code attempt is below, and the original attempt is below that
>>> from my OP.
>>>
>>> var arr = map.getLayersByName("Vector layer");
>>>
>>> if (arr.length) {
>>> //map.removeLayer(arr[0]);
>>> layer.removeAllFeatures();
>>> }
>>> else {
>>> layer = new OpenLayers.Layer.Vector("Vector layer", {styleMap:
>>> myStyles});
>>> }
>>>
>>> Neither `map.removeLayer(arr[0])` nor `layer.removeAllFeatures()` works.
>>>
>>> Would appreciate suggestions.
>>>
>>> On Jan 19, 2012, at 3:00 PM, Puneet Kishor wrote:
>>>
>>>> I am trying to remove a layer with
>>>>
>>>> if (map.getLayerIndex(layer) > 0) {
>>>> map.removeLayer(layer);
>>>> }
>>>>
>>>> but no joy. How can I remove a layer?
>>>>
>>>> A bonus question: if I want to reconstruct a vector layer on map
>>> "zoomend", that is, redraw all the features on it so they are resized
>>> accordingly, should I remove the layer (hence, the question above), or
>>> simply remove all the features and redraw the features? If the latter, then
>>> how?
>>>>
>>>> Many thanks,
>>>>
>>>> --
>>>> Puneet Kishor
>>>
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>>>
>>
>>
>>
>> --
>> I.R
>
More information about the Users
mailing list