[OpenLayers-Users] Implementing OpenLayers in JavaScript object: scope issues

gingerbbm stuart.jones at jacobs.com
Thu Jan 22 12:11:47 EST 2009


Dear all

I am implementing an OpenLayers map in an existing project and I want to
separate the mapping functionality into its own JavaScript namespace, e.g.

    var MyProject.Mapping = {
        map: null,
        initialise: function() {
            this.map = new OpenLayers.Map('map', mapOptions);
            // etc
        },
        // etc
    }

The code is being moved from a working non-namespaced implementation, and I
believe I'm getting problems to do with scope. I have a property of my
namespace object defined as follows:

    drawTools: {
        toolDrawPolygon: new OpenLayers.Control.DrawFeature(
            this.layerVector,
            OpenLayers.Handler.Polygon,
            {
                featureAdded: function() { alert('Polygon drawn'); },
                handlerOptions: {
                    createFeature: function() {
                        MyProject.Mapping.layerVector.destroyFeatures();
                       
OpenLayers.Handler.Polygon.prototype.createFeature.apply(this, arguments);
                    }
                }
            }
        )
    };


On the click of a button I call
MyProject.Mapping.drawTools.toolDrawPolygon.activate() after which I can
happily draw a polygon on the map. But when I double-click to complete the
shape I get an error:

    this.layer is undefined                      OpenLayers.js (line 1051)

The question is, why? I appreciate this is specifically a JavaScript/scope
problem but if you could give me any pointers I'd be very grateful.

Thanks
Stuart
-- 
View this message in context: http://n2.nabble.com/Implementing-OpenLayers-in-JavaScript-object%3A-scope-issues-tp2198920p2198920.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.




More information about the Users mailing list