OpenLayers选择具有多边形的要素

时间:2013-03-12 10:20:32

标签: openlayers

尝试使用此功能 - http://openlayers.org/dev/examples/SLDSelect.html 例如: 我有选择功能(框),允许我在图层上选择功能。但它是盒子类型。 现在我想尝试相同的选择,但多边形样式。就像绘制新的多边形一样,但是多边形内部会选择什么。 我是否试图使用这种SLD选择正确的路径,还是有其他更好的方法?

对所有解决方案开放。

感谢。

2 个答案:

答案 0 :(得分:1)

在这个plunker中,我使用了interaction.Draw来创建一个多边形。完成多边形后,它会在多边形的范围内找到所有要素点并选择它们。

Here is a link to the plunker

以下是draw.on('drawend',...听众的摘录:

draw.on('drawend', function(e) {

  e.preventDefault();
  //stop a click select from overriding selection made by polygon
  setTimeout(function(){
    select.setActive(true)
  },300);

  // features that intersect the box are added to the collection of
  // selected features, and their names are displayed in the "info"
  // div
  var extent = e.feature.getGeometry().getExtent();

  //pointsLayer is the vector layer with the point features
  pointsLayer.getSource().forEachFeatureIntersectingExtent(extent, function(feature) {
    selectedFeatures.push(feature);
  });

});

答案 1 :(得分:0)

来自GIS Stack Exchange站点:

function buildIt() {//START FUNCTION buildIt

//CREATE A NEW EMPTY VECTOR LAYER 
var polygonAdHoc = new OpenLayers.Layer.Vector("Poly Layer");
//ADD THE NEW VECTOR LAYER TO THE OPENLAYERS MAP
map.addLayer(polygonAdHoc);
//SET A VARIABLE TO THE NAME OF THE EXISTING LAYER THAT WILL BE TESTED FOR INTERSECTION WITH THE USER CREATED POLYGON
//I CHOSE TO GET THE LAYER BY NAME BUT YOU MIGHT CHOOSE TO DO IT ANOTHER WAY
var standLyr = map.getLayersByName("nameOfTheVectorLayerYouWantToTestForIntersection");

//CREATE A DRAW FEATURE CONTROL FOR THE USER CREATED VECTOR LAYER
var draw = new OpenLayers.Control.DrawFeature(polygonAdHoc, OpenLayers.Handler.Polygon);
//ADD THE DRAW FEATURE CONTROL TO THE MAP
map.addControl(draw);
//ACTIVATE THE DRAW FEATURE CONTROL
draw.activate();

//WHEN THE USER FINISHES DRAWING THE AD-HOC POLYGON THE beforefeatureadded EVENT WILL FIRE
polygonAdHoc.events.on({
    beforefeatureadded: function (event) {
        poly = event.feature.geometry;//SET A VARIABLE TO THE USERDRAWN POLYGONS GEOMETRY
        //alert("polygonAdHoc.features[0].geometry: " + poly);//IF YOU WANT TO SEE THE GEOMETRY COORDINATES UNCOMMENT THIS LINE
        for (var a = 0; a < standLyr[0].features.length; a++) {//LOOP THRU THE STANDS FEATURES OF THE LAYER YOU WANT TO TEST FOR INTERSECTION WITH THE USER DRAWN POLYGON
            if (poly.intersects(standLyr[0].features[a].geometry)) {//IF THE USER DRAWN POLYGON INTERSECTS THE TARGET LAYERS FEATURE REPRESENTED BY THE VARIABLE "a" THEN
                 //IDENTIFY THE FEATURE THAT INTERSECTS 
                 //FOR SIMPLICITIES SAKE I CHOSE TO JUST FIRE AN ALERT
                 //MY ACTUAL APP ADDS EACH SELECTED FEATURE TO A SELECT FEATURE CONTROL AND HIGHLIGHTS EACH POLYGON ON THE MAP                
                 alert("stands feature intersection: " +  standLyr[0].features[a].attributes['nameOfAttribute']);
            }//END OF IF STATEMENT
        }//END OF FOR STATEMENT
        draw.deactivate();//I ONLY WANT THE USER TO BE ABLE TO DRAW ONE AD-HOC POLYGON
        //SO I DEACTIVATE THE DRAW FEATURE CONTROL AFTER THEY CREATE THE FIRST POLYGON
        return false;
    }//END OF beforefeatureadded FUNCTION
});//END OF polygonAdHoc.events.on
}//END OF buildIt FUNCTION