2014-06-10 4 views
0

해당 특성을 기반으로 기능 레이어를 선택하는 데 문제가 있습니다. 나는 아래의 코드를 가지고 있지만 말한다 :Openlayer에서 내부 함수를 호출하는 방법은 무엇입니까?

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

+0

showparcel 또는 실제 마우스 이벤트로 선택 하시겠습니까? –

+0

어느 쪽이든 .. 나는 속성에 따라 소포를 보여주고 싶습니다. '지도에서 소포 표시'버튼을 클릭하면 해당 PIN이있는 구획/지형지 물이 선택됩니다. –

+0

우리는 이것을 gis 섹션으로 검토해야합니다. –

답변

0

해당 색인의 getFeaturesByAttribute 또는 track 기능을 해당 색인의 FID와 함께 사용하고 getFeatureByFid를 사용해야합니다.

나는 보통 내 자신의 개체 또는 해시 테이블에서 추적 한 다음 FID로 참조하는 것을 선호합니다.

예에서 openlayers 외부에서 자신을 검색 할 수있는 고유 ID를 가져온 다음 getFeaturesByAttribute를 사용하여 존재하는 고유 ID를 참조하십시오. 그게 감각이 없다면 나는 그 의견에 나를 때렸다.

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

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

+0

나는 이미 OpenImage에서 링크를 방문했으며 getFeatureByAttribute를 구현하는 방법에 대한 예제 (코드)를 찾을 수 없습니다. 어떤 링크? –

+0

해당 메서드에 대한 openlayers 데모는 없지만 위의 작은 예제는 사용자가 원하는 것을 수행합니다. layer_agao.getFeaturesByAttribute ("newpin", "12345") [0]. 이 방법의 가장 어려운 부분은 찾고자하는 속성 값이 고유해야한다는 것입니다. –

+0

중첩 된 기능을 사용하여 특성별로 검색을 구현할 수 있습니다. 이 [link] (http://stackoverflow.com/questions/8817872/javascript-call-nested-function)이 도움이됩니다. –

0

Vector.Layer에 수신기를 추가하는 정확한 방법 (유형, OBJ, 수신기) 소스의 설명에서와 같이 layer.events.register이다 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은 보이지 않는다.

관련 문제