0

In my database table there are four fields(lat,lon,sensid,value).i am displaying a vector layer(point feature) by retrieving the lat, lon values from database. Now i want to display the value, whenever i click on the feature based on id. So how to get the feature id(database sensid) on click.I am getting lat lon values on click, but i need to get sensid.

my code is

var  str_array =["8541652,3397019,TATM1", "8786758,3536158,TATM2"]
 str_array = jsstr.split(';');
         var a,b,c;
    for (i = 0; i < str_array.length; i++) { 
   var text = str_array[i] ;
    var  str_coord = text.split(',');
     a = parseFloat(str_coord[0]);
     b = parseFloat(str_coord[1]);
  c = parseFloat(str_coord[2]);
var feature=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(a,b));

           vectorLayer.addFeatures(feature);
     mapPanel.map.addLayer(vectorLayer);
var selectCtrl = new OpenLayers.Control.SelectFeature(vectorLayer);


    function createPopup(feature) {

        popup1 = new GeoExt.Popup({
            title: 'My Popup',
            location: feature,
            width:200,
            html: '<div class="markerContent">'+feature.geometry.x +','+feature.geometry.y+'</div>',
            maximizable: true,
            collapsible: true,
            anchorPosition: "auto"
        });
       // unselect feature when the popup
       // is closed
        popup1.on({
            close: function() {
                if(OpenLayers.Util.indexOf(vectorLayer.selectedFeatures,
                                           this.feature) > -1) {
                    selectCtrl.unselect(this.feature);
                }
            }
        });
        popup1.show();
    }

   //create popup on "featureselected"


    mapPanel.map.addControl(selectCtrl);
   selectCtrl.activate();
}
vectorLayer.events.on({
        featureselected: function(e) {

            createPopup(e.feature);

        }
    });
1
  • 1
    you are getting 'feature.geometry', call 'feature.sensid' instead Commented Jul 14, 2016 at 5:36

1 Answer 1

1

To get the attribute data of specific feature in case you are getting the 4 fields from DB to a layer in your project, then use attributes object as below:

function createPopup(feature) {
       var sensid = feature.attributes.sensid;
       // and add it in the html of your popup
       popup1 = new GeoExt.Popup({
        title: 'My Popup',
        location: feature,
        width:200,
        html: '<div class="markerContent">'+sensid+'</div>',
        maximizable: true,
        collapsible: true,
        anchorPosition: "auto"
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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