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

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

本文关键字:图层 获取      更新时间:2023-09-26

我有以下问题:选择此功能时,如何获取特征的名称(例如点)?我有一个函数,在其中我声明矢量层和特征及其名称(代码的一部分):

  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);  
    }
  }

然后,我想有第二个功能来提醒我选择的功能的名称。像这样:

   function onFeatureSelect(){
         alert(featureName);
    }

有可能做这样的事情吗?我希望我的问题足够清楚。谢谢迪米特里斯

您可以使用OpenLayers.Feature.Vector中的属性:

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

通过这种方式,您可以指定所需功能的名称,如以下示例所示:

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

希望这有帮助,