如何在Openlayer中调用内部函数?

时间:2014-06-10 11:45:25

标签: javascript

我无法根据其属性选择要素图层。我在下面得到了这个代码,但它说:

Uncaught TypeError: Cannot read property 'features' of undefined

这是我的代码:

 var init = function () { // A function that will initialize and execute all the declared variables
    var geographic = new OpenLayers.Projection("EPSG:4326"); // Setting the standard geographic projection
    var mercator = new OpenLayers.Projection("EPSG:3857"); // Setting the universal geographic projection
    map = new OpenLayers.Map('map'); // Creating & initializing map constructor
    var base_osm = new OpenLayers.Layer.OSM("OpenStreetMap"); // Setting OpenStreetMap as a BaseMap

    map.addControl(
            new OpenLayers.Control.MousePosition({
                prefix: '<small style="color:blue">',
                suffix: '</small>',
                numDigits: 2,
                emptyString: '<small style="color:red">' + 'Mouse is not over map.' +'</small>'
            })
    );

    var layer_agao = new OpenLayers.Layer.Vector("Agao");
    map.addLayers([layer_agao, base_osm]); // Adding the vector layer to the map
    map.addControl(new OpenLayers.Control.LayerSwitcher());

    selectControl = new OpenLayers.Control.SelectFeature(layer_agao, {
         onSelect: onFeatureSelect, onUnselect: onFeatureUnselect
    });

    map.addControl(selectControl);
    selectControl.activate();
    map.setCenter(new OpenLayers.LonLat(13975400.3513, 999830.692078),16);

    var format_agao = new OpenLayers.Format.GeoJSON(); //initializing and calling the rendered GeoJSON Layer from views.py
    var feat_agao = format_agao.read({{agao_transform|safe}});
    layer_agao.addFeatures(feat_agao);
    layer_agao.events.on({
            featureselected: function(event) {
                 var feature = event.feature;
                 var area = feature.geometry.getArea();
                 var id = feature.attributes.newpin;
                 var output = "Land Pin: " + id + "<br/>" + "Area: " + area.toFixed(12);
                    document.getElementById("status").innerHTML = output;
            }
    });
init.showparcel();
}

init.showparcel = function (getpin){
            for(var f=0;f<layer_agao.features.length;f++) {
                if(layer_agao.features[f].attributes.newpin == getpin) {
                    selectControl.select(layer_agao.features[f]);
                    break;
                }
            }
}

我也读过关于getfeaturesbyattribute的内容,但我找不到任何例子。那么,还有其他方法可以在点击(事件)上调用特定的要素图层吗?这是我的搜索... when i clicked the "display parcel on map" a specific feature should be selected

2 个答案:

答案 0 :(得分:0)

您需要使用自己索引中的getFeaturesByAttribute或跟踪功能,并将其FID作为该对象的索引,然后使用getFeatureByFid。

我通常更喜欢在我自己的对象或哈希表中跟踪它们,然后通过FID进行引用。

在您的示例中,我将在attribs上引入一个唯一ID,您可以在openlayers之外搜索自己,然后使用getFeaturesByAttribute引用您知道存在的唯一ID。如果那不可能在评论中打击我。

vlayer.getFeaturesByAttribute("fid", target)[0]

http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.getFeaturesByAttribute

答案 1 :(得分:0)

向Vector.Layer添加侦听器的正确方法是layer.events.register(type,obj,listener),如源代码中的注释所示:http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Layer/Vector.js。请注意,featureselected的侦听器将传递所选的功能,而不是您拥有的事件。

所以,在你的情况下:

layer_agao.events.on('featureselected', null, function(feature){
   //do something with the feature
   var area = feature.geometry.getArea();
   var id = feature.attributes.newpin;
   var output = "Land Pin: " + id + "<br/>" + "Area: " + area.toFixed(12);
                document.getElementById("status").innerHTML = output;
});
根据您的代码示例,

getFeaturesByAttribute看起来并不像您需要的那样,尽管它在特定情况下很有用。