0

I have trouble about selecting the feature layer based on its attribute. I got this code below but it says:

Uncaught TypeError: Cannot read property 'features' of undefined

here's my code:

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

I also read about getfeaturesbyattribute, but i can't find any example. So, is there other way to call the specific feature layer on click (event)? This is for my searching...when i clicked the "display parcel on map" a specific feature should be selected

Sachi Tekina
  • 1,800
  • 5
  • 28
  • 50

2 Answers2

0

You would need to use the getFeaturesByAttribute or track features in your own index with their FID as the index of that object, and then use getFeatureByFid.

I usually prefer to track them in my own object or hashtable and then reference by FID.

In your example I would pull in an unique id on the attribs that you can search yourself outside of openlayers, and then use the getFeaturesByAttribute to reference the unique id that you know exist. If that doesn't may sense hit me up in the comments.

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

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

Frank Phillips
  • 95
  • 1
  • 11
  • I visited that link already, I'm new in Openlayer, and I can't find any example (code) on how to implement getFeatureByAttribute. Any links? – Sachi Tekina Jun 16 '14 at 00:18
  • There is not an openlayers demo on that method, but the small example above will do what you are looking for. layer_agao.getFeaturesByAttribute("newpin", "12345")[0]. The most challenging part of this method is that the attribute value you are looking for needs to be unique. – Frank Phillips Jun 16 '14 at 13:51
  • I manage to implement the search by attribute by using nested function. This [link](http://stackoverflow.com/questions/8817872/javascript-call-nested-function) is helpful. – Sachi Tekina Jun 17 '14 at 07:55
0

The correct way to add a listener to a Vector.Layer is layer.events.register(type, obj, listener) as shown in the comments in the source: http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Layer/Vector.js. Note, the listener for featureselected is passed the selected feature, not the event as you have it.

So, in your case:

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 doesn't look like it is what you need, based on your code sample, though it is useful in specific cases.

John Powell
  • 12,253
  • 6
  • 59
  • 67