简体   繁体   English

如何在“开放层”中获取矢量层的要素(点)名称?

[英]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 感谢Dimitris

you can use the properties from OpenLayers.Feature.Vector: 您可以使用OpenLayers.Feature.Vector中的属性:

http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.Properties 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 https://gis.stackexchange.com/questions/40689/how-to-show-a-toolip-over-a-feature-with-openlayers

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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