codeBlog code is only worth when it is working

25Jun/150

openlayers draw polygon

var layerDataType = "User Defined Polygon Layer";
var polygonLayer;

var map = new OpenLayers.Map('map');

// Initialize the polygon editor.
var polygonEditor ;

// Initialize the polygon selector.
var polygonSelector;

var wmsLayer;

var USDPolygonModifyShape;

var USDPolygonRegular;

function buildAllObject(){
layerDataType = "User Defined Polygon Layer";
polygonLayer = new OpenLayers.Layer.Vector(layerDataType, {
projection: "EPSG:4326"
, pointRadius: 6
,renderers: ['Canvas', 'VML']
});

// Initialize the polygon editor.
polygonEditor = new OpenLayers.Control.DrawFeature(polygonLayer,OpenLayers.Handler.Polygon);

// Initialize the polygon selector.
polygonSelector = new OpenLayers.Control.SelectFeature(polygonLayer,{
clickout: true, toggle: true,
multiple: false, hover: false,
toggleKey: "ctrlKey", // ctrl key removes from selection
multipleKey: "shiftKey", // shift key adds to selection
box: true
});

wmsLayer = new OpenLayers.Layer.WMS("OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0?",
{layers: 'basic'},
{'attribution': 'Provided by OSGeo'}
);

USDPolygonModifyShape = new OpenLayers.Control.ModifyFeature(polygonLayer);

USDPolygonRegular = new OpenLayers.Control.DrawFeature(polygonLayer,
OpenLayers.Handler.RegularPolygon,{
handlerOptions: {
irregular: true
}
});
}
function newUSDPolygonValue(){

if(polygonSelector) {
map.addControl(polygonSelector);
polygonSelector.deactivate();
}

if(USDPolygonModifyShape){
USDPolygonModifyShape.deactivate();
}

if(USDPolygonRegular){
USDPolygonRegular.deactivate();
}

// Add a polygon layer to which polygons will be drawn.

map.addLayers([polygonLayer]);
// And its event listener when the feature is added.
polygonEditor.events.register('featureadded', polygonEditor, function(evt) {

// Here, we should see the geometry of the drawn feature in your console.
var geom = evt.feature.geometry;
console.log(geom);
polygonLayer.refresh({force:true});
});

polygonEditor.handler.callbacks.point = function(data) {

if(polygonLayer) {
polygonLayer.refresh({force:true});
polygonLayer.removeAllFeatures();
}
}

map.addControl(polygonEditor);

// Activate the control.
polygonEditor.activate();

}

/* function to Draw Ploygon

function drawPolygon (){
buildAllObject();
newUSDPolygonValue();
});

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS