OpenLayers选择多边形特征



尝试使用这个- http://openlayers.org/dev/examples/SLDSelect.html例如:我有选择特征(框),允许我选择图层上的特征。但它是盒式的。现在我试着做相同的选择,但多边形风格。比如画一个新的多边形,但是这个多边形里面的东西会被选中。我是在正确的道路上尝试使用这个SLD选择还是有其他更好的方法?

对任何解决方案开放

谢谢。

在这个柱塞中我使用了交互。绘制以创建一个多边形。一旦多边形完成,它会找到多边形范围内的所有特征点并选择它们。

这是活塞的链接

以下是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);
  });
});

来自GIS StackEchange网站:

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

最新更新