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 add several vector layers (KML content got from jQuery GET) in this way:

var layer1Vector= new OpenLayers.Layer.Vector('activeInfo');

It works quite well.

Instead of adding the

selectControlKml = new OpenLayers.Control.SelectFeature(
    [layer1, layer2, ***], {
        clickout: true, toggle: false,
        multiple: false, hover: false,
        toggleKey: 'ctrlKey',
        multipleKey: 'shiftKey'

I knew that it's possible to add a "general" click handler event

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
        'single': true,
        'double': false,
        'pixelTolerance': 5,
        'stopSingle': true,
        'stopDouble': false,
        'delay': 0
    initialize: function(options) {
        this.handlerOptions = OpenLayers.Util.extend({}, this.defaultHandlerOptions);
        OpenLayers.Control.prototype.initialize.apply(this, arguments); 
        this.handler = new OpenLayers.Handler.Click(
            this, {'click': this.trigger}, 
    trigger: function(e) {

        if ( {
            //var feature = vectorLayer.getFeatureById(;
            alert('click on fea' +
            //$('fill', '#000000');
            //$('cursor', 'wait');
            //$("div#info").append("<span>You just clicked on " + + "<span><br />");

If I click on a vector layer I have to open different custom popups (qTip2). The code:

if ( {

works, but how can I get the layer name based on the e object? I analyzed all the methods/properties, but without having success.

E.g. One of my layers was defined as:

var layer1Vector= new OpenLayers.Layer.Vector('activeInfo');

I need to get 'activeInfo'.

Thanks a lot!

share|improve this question
up vote 2 down vote accepted

Since this is the vector in this context... will give you "activeInfo".

trigger: function(e) {

        if ( {

here's my console.log(this) within trigger function.

enter image description here


So your problem is that you're trying to assign the click handler before the layer has been created.

This Works. Here I have moved the call to the addControls(vectorLayer) function after you add the layer. This works fine. Notice I've added a layer parameter. This will make it easy for you when you're adding and removing layers.

Once you remove a vector Layer the handler is gone with it. If you add another layer, simply call the addControls(yourNewLayerName) function to add the handler to the new layer.

share|improve this answer
No, it's undefined the .name property. It does not exist checking with FF Firebug. Now I'm analyzing the this object. – Daviddd Sep 19 '12 at 15:12
Works for me. Here's my demo test so you can compare code. – CaptDragon Sep 19 '12 at 15:24
First of all thanks! The vector layers are loaded dynamically using jQuery. I add the clickHandler function before loading the vectors, probably this could be the reason. I'm doing several tests, I inform you later! – Daviddd Sep 20 '12 at 6:23
This is my problem: Demo test problem. I need to update the ClickHandler, but how? – Daviddd Sep 20 '12 at 6:55
:-) I'm crying...thanks I got it! – Daviddd Sep 20 '12 at 13:48

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.