1

I use OpenLayers 2.13.1 along with GeoServer and PostgreSQL

I have a code for a vector layer in my OpenLayers like so

//style for layer
var style_d = new OpenLayers.Style({
 'fillColor': '${p_color}',
 'fillOpacity': .8,
 'fontColor': 'black',
 'pointRadius': 8,
 'strokeColor': 'black',
 'strokeWidth': 1,
 'fontFamily': 'Arial',
 'fontSize': 10,
 'label': '${p_name}',
 'labelAlign': 'tc',
 'labelSelect': true,
 'labelXOffset': 4,
 'labelYOffset':4,
 },
 {
    context: {
        label:function(feature) {
            if(feature.attributes['p_name']) {
                return feature.attributes.p_name;       
            } else {
                return '';
            }
        }
 },
});


//style for layer - when click a geometry
var style_select = new OpenLayers.Style({
 'fillColor': '${p_color}',
 'fillOpacity': .4,
 'fontColor': 'black',
 'pointRadius': 5,
 'strokeColor': 'black',
 'strokeWidth': .5,
 'fontFamily': 'Arial',
 'fontSize': 10,
 'label': '${p_name}',
 'labelAlign': 'tc',
 'labelSelect': true,
 'labelXOffset': 4,
 'labelYOffset':4,
 },
 {
    context: {
        label:function(feature) {
            if(feature.attributes['p_name']) {
                return feature.attributes.p_name;
            } else {
                return '';
            }
        }
 }
});

//Add a select feature control for pins
var select_feature_control = new OpenLayers.Control.SelectFeature(mylayer, {multiple: false,toggle: true});

map.addControl(select_feature_control);

//Activate the control
select_feature_control.activate();

//do stuff when clicked
function selected_feature(event){
 document.getElementById("wesmes").innerHTML="Loading";
 sf = event.feature.fid;
 var sfs = sf.split(".");
 var sff = sfs[1];
 //call a function
}

Problem :

If there is a polygon or a line on the map I can click right on them and everything works.

If there is a point, I have to click outside the point, near at it, so it will be clicked and activate the function selected_feature(event) etc etc. If I click on the point, nothing happens

How can I fix this? Why is this happening? I think it is a problem of style. If you want I can provide more code

1

If you have a Vector Layer array of elements and if you want doing something when one of the array elements is clicked, I'm doing this:

var vectorLayer = new OpenLayers.Layer.Vector("Valori", {
    eventListeners:{
        'featureselected':function(evt){                
              alert("Element clicked!");
        },
        'featureunselected':function(evt){
              //whatever
        }
    }
});

And it works fine.

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.