简体   繁体   中英

Openlayers 2. z-axis and select control

I've got a couple vector layers one has polygons, one has lines. We have a need to add lines to the line layer that attach to the polygons (database procedure requires the polygons IDs, which are stored in attributes on the polygons)

So I have a drawFeature control on the lineLayer, and a selectFeature (which stores off the ID on hover rather than just selecting) on the polygonLayer. It actually works just fine except the z axis of the linelayer while added is lower, so it shows the new line being drawn under the polygon. Would rather have the line show over the polygon. I know it's because when the selectFeature control is active it's setting the z-index of the polygon layer higher than the lineLayer. I manually set the z-index of the linelayer higher than the polygon layer using lineLayer.setZIndex(800) or whatever, and that certainly makes the new line draw over the polygons, but then the selectFeature events don't trigger. I've considered several solutions including adding the drawFeature to my polygon layer and then moving the feature to the line layer when done, but it's still rendering under the polygons, I even played with the graphicZIndex on the "temporary" style for my stylemap on the polygon layer. to no avail (i did set the renderOptions zindexing to true on the polygon layer)

I may be approaching this from the wrong angle, so I'm open to suggestions. If there was a function available on the vector layer something like getFeatureByPosition(position), I could grab the position on sketchStarted, and sketchEnded events and query that, but so far I've been unable to find anything like that.

I'm not on my dev box at the moment in case anyone is wondering why no code. Wanted to post this from work, but the base network is having issues displaying the logon page (due to ssl I think)

So my solution required a few things. First I needed to upgrade from OL 2.10 to the latest 2.13.1. Mostly this was due to needing a new event that was added to 2.11 (I think or maybe 2.12) the event is "featureover" which can be caught at the map level and therefore will trigger on all layers, so I'm not fighting with Z-Index of the select. I removed the select control from the polygon layer as it was not needed.

var featureOverHandler = function(event){
    if (event.feature.layer.id == polygonLayer.id) {
        selectedPolygonId = event.feature.attributes.POLYGON_ID;
        console.log("Selected Polygon Id: " + selectedPolygonId);
        map.events.unregister('featureover',map,featureOverHandler);
        map.events.register('featureout',map,featureOutHandler);
    }
};
var featureOutHandler = function (event) {
    if (event.feature.layer.id == polygonLayer.id) {
        selectedPolygonId = 0;
        console.log("Cleared Selected Polygon ID ");
        map.events.unregister('featureout', map, featureOutHandler);
        map.events.register('featureover', map, featureOverHandler);
    }
};

These will catch the polygon that is currently hovered over. But then I add events to catch where the line starts and ends. Since as of 2.11, they changed the way the "sketchstarted" event works, you can non longer use that to capture which polygon the pointer was over when the first point was added. I used the point callback on the vector layer.

var vDrawOptions = {
    callbacks: {
        "point": function (p) {
            if (p.parent.components.length == 2) {
                console.log("First Point added");
                startingPolygonId = selectedPolygonId;
            }
        }
    }
}
vectorAddControl = new OpenLayers.Control.DrawFeature(vectorLayer, OpenLayers.Handler.Path,vDrawOptions);

however, the "skecthcomplete" can still be used to capture the ending point (and thus polygon)

function vSketchComplete(evt) {
    endingPolygonId = selectedPolygonId;
};
vectorLayer.events.register('sketchcomplete', vectorLayer, vSketchComplete);

Hopefully this will help someone else, in a similar situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM