简体   繁体   中英

How to get the name of a feature (point) of a vector layer in Open Layers?

I have the following question: How can I get the name of a feature (eg point) when I select this feature? I have a function in which I declare the vector layer and the features with their names (PART OF THE CODE):

  function makeLayer(){

    var objPoints = {station1: '68.0226656 36.9819691',station2: '66.895908 38.67347'};
    // loop through the object with the points
    for (var pointStat in objPoints ){
        var pointCoords = objPoints[pointStat]
        // seperate the coordinates lat and lon
        var PosSpace=pointCoords.indexOf(' ');
        var lonStr = pointCoords.substring(0,PosSpace);
        var lon = +(lonStr); //convert string to number
        var latStr = pointCoords.substring(PosSpace+1);
        var lat = +(latStr);
        // create the geometry
                    var point = new OpenLayers.Geometry.Point(lon,lat);
        // assign the geometry to the feature
                    var feature_point = new OpenLayers.Feature.Vector(
        point,
        {name: pointStat} // name of label
        );
        // add the generated feature to the vector layer
        this.layer.addFeatures(feature_point);  
    }
  }

Then, I want to have a second function where I alert the name of the feature which I selected. Something like this:

   function onFeatureSelect(){
         alert(featureName);
    }

Is it possible to do something like this? I hope my question is clear enough. Thanks Dimitris

you can use the properties from OpenLayers.Feature.Vector:

http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.Properties

in that way you can specify the name of the feature you want, something like in this example:

https://gis.stackexchange.com/questions/40689/how-to-show-a-toolip-over-a-feature-with-openlayers

Hope this helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM