2

I'd like to create a map displaying a feature ready to modify. The map should already be selected and show the draggable vertices when you load the page (without needing to click on the feature first).

With this code, you first need to click the feature before you can edit:

    layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    vectorLayer = new OpenLayers.Layer.Vector("Foo");
    map = new OpenLayers.Map('map');
    var wkt = "POLYGON((0 1, -1 0, 1 0))";
    var polygonFeature = new OpenLayers.Format.WKT().read(wkt);
    vectorLayer.addFeatures([polygonFeature]);
    map.addLayers([layer, vectorLayer]);
    map.zoomToExtent(vectorLayer.getDataExtent());
    modifyControl = new OpenLayers.Control.ModifyFeature(vectorLayer);
    map.addControls([modifyControl]);
    modifyControl.activate();    

I already tried OpenLayers.Control.SelectFeature. It selects the feature (blue line) but it doesn't show the draggable vertices:

    selectFeature = new OpenLayers.Control.SelectFeature(vectorLayer);
    selectFeature.select(vectorLayer.features[0]);
    selectFeature.activate();

Any ideas? Thanks!

2

You have two ways for that:

First: ModifyControls have their own SelectFeature control, you can use it:

modifyControl.selectControl.select(vectorLayer.features[0]);

Second way: You can use standalone mode and selectFeature() method: http://dev.openlayers.org/docs/files/OpenLayers/Control/ModifyFeature-js.html#OpenLayers.Control.ModifyFeature.selectFeature

But you can't unselect and select features with mouse in standalone mode (you can do it only with code), so I'm not sure, whether it's suitable for your needs.

modifyControl = new OpenLayers.Control.ModifyFeature(vectorLayer, {standalone: true});
/ ... /

modifyControl.selectFeature(vectorLayer.features[0]);
| improve this answer | |
  • Thank you! modifyControl.selectControl.select(vectorLayer.features[0]); does exactly what I want! – Mark Dec 27 '12 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.