1

I have been struggling with zoom to layer functionality with event listeners. The code is working and zoom to layer whenever it is loaded but the problem is that once it is loaded, i can not zoom or pan to other areas, and if i have more than 2 layers loaded with event listeners then it become messy. How do I remove or deactivate the event listener as soon the layer is loaded...Here is the code

var HSlay06 = new OpenLayers.Layer.MapServer('<b>Human Settlement 2006</b>',

                "http://localhost/cgi-bin/mapserv?map=/var/www/html/mapfile/Human_Settlement2006.map", 
                {
                    map: "/var/www/html/mapfile/Huma_Settlement2006.map"
                },
                { 
                    strategies: [new OpenLayers.Strategy.BBOX()],
                    eventListeners: {           
                        'loadstart': function (evt) 
                        {
                                map.zoomToExtent(HSExtent);
                        }
                    }
                },
                {
                    transparent: true,
                    numZoomLevels: 22,
                    opacity: 0.5,
                    visibility: false,
                    isBaseLayer: false
                }
            );
1

You could try to check if the Layer is loaded the first time:

var first_time=true;

var HSlay06 = new OpenLayers.Layer.MapServer('Human Settlement 2006',

            "http://localhost/cgi-bin/mapserv?map=/var/www/html/mapfile/Human_Settlement2006.map", 
            {
                map: "/var/www/html/mapfile/Huma_Settlement2006.map"
            },
            { 
                strategies: [new OpenLayers.Strategy.BBOX()],
                eventListeners: {           
                    'loadend': function (evt) 
                    {
                           if(first_time===true)
                            {
                            map.zoomToExtent(HSExtent);
                            }
                            first_time=false;
                    }
                }
            },
            {
                transparent: true,
                numZoomLevels: 22,
                opacity: 0.5,
                visibility: false,
                isBaseLayer: false
            }
        );
  • Wow and it worked like a charm but I can only load once...I am happy – Asanda Lamba Jun 18 '15 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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