Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a selectFeature control for a point layer in OpenLayers 2.12. I have a problem after removing the selectFeature control from the map.

The reason why I want to remove the control is because the target layer of the control is destroyed and a new one is added every time the map is moved.

In this process I also remove the selectFeature control from the map and add a new one for the updated layer. But that does not work properly. After the first move/zoom event of the map get the following error:

Error: getFeatureFromEvent called on layer with no renderer. This usually means you destroyed a layer, but not some handler which is associated with it.

Here is a version of the function refresh, which is called on any pan/zoom event of the map:

var mylayer;
var mycontrol;
refresh = function(){

     // Remove previous layers and controls
    if(mylayer != null){

    // Get mylayer
    mylayer = new OpenLayers.Layer.Vector("GeoJSON", 

    // Add layer and selectFeature control
    mycontrol = new OpenLayers.Control.SelectFeature(mylayer,{  
        onSelect: function(e) {
        $("#tmpOutput1").html( + "\n" +;



To solve the problem I had to change the layer/control removal to this:

if(mylayer != null){


Note that here the order matters (at least in my example): first remove the control, then remove the layer.

share|improve this question
up vote 3 down vote accepted

In my opinion there is an issue with layer removal. To my understanding the function OpenLayers.Layer.destroy() will not remove the layer from map; according to the API docs, its mission is "to alleviate cyclic references which the Javascript garbage cleaner can not take care of on its own" (

To remove a layer, after calling the destroy() function, you must call the function OpenLayers.Map.removeLayer() (

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.