[OpenLayers-Users] Using custom Feature class

nick@hogweed.org nick at hogweed.org
Tue Jul 4 05:23:22 CDT 2006


Following on from my experiments with tiled markers, it seems that the Feature 
class is designed to combine markers and popups in one, which is what I need.

So I have changed my code slightly to create a new class inherited from 
Feature.
What I want to do is make the popup associated with the feature appear when 
the user clicks on it. The event works (an alert box in the event handler 
appears) but I run into problems when trying to display the popup.

My feature class (nick.dev.openstreetmap.org/openlayers/osmfeature.js) is as 
follows:

OpenLayers.Feature.OSMtest = Class.create();
OpenLayers.Feature.OSMtest.prototype =
    Object.extend (new OpenLayers.Feature(), {

        thePopup: null,
        currentObj : null,

        initialize: function (layer,lonLat,data) {
            OpenLayers.Feature.prototype.initialize.apply(this,arguments);
            this.createMarker();
            this.thePopup = this.createPopup();

	    // this works
            if(this.thePopup)
                alert('popup created successfully');

            this.layer.addMarker(this.marker);
            this.marker.events.register('click',  this.marker,
                                this.showPopup);
            currentObj = this;

        },

        destroy: function() {
            if (this.marker != null) {
                this.layer.removeMarker(this.marker);
            }
            OpenLayers.Feature.prototype.destroy.apply(this,arguments);
        },

        showPopup: function() {
            alert('feature was clicked!');  // this works
            //this.thePopup.draw();
            currentObj.thePopup.draw();
        }

Note that I have added the 'currentObj' as I ran into 'no properties' errors 
with 'this.thePopup.draw()'. I figured this might be a scoping problem with 
'this' again (as a result of the 'onclick' call?) so I used 'currentObj' as 
an alternative reference for 'this'.
However now I get an error:

Error: this.map has no properties
Source File: http://openlayers.org/api/OpenLayers.js
Line: 359

As well as the error, another question.... am I going about this the right 
way, or is there a cleaner, automated way to associate features with popups, 
so that the popup associated with the feature automatically comes up when you 
click on the feature's marker?

Thanks,
Nick


More information about the Users mailing list