OpenLayers 添加RegularPolygon
1、主要涉及类:
OpenLayers.Layer.Vector、OpenLayers.Control.DrawFeature、
OpenLayers.Handler.RegularPolygon
2、方法编写
var polygonControl ; function addPolygonLayer(){ var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer"); map.addLayers([polygonLayer]); polyOptions = {sides: 4}; polygonControl = new OpenLayers.Control.DrawFeature(polygonLayer, OpenLayers.Handler.RegularPolygon,{handlerOptions: polyOptions}); map.addControl(polygonControl); map.setCenter(new OpenLayers.LonLat(0, 0), 3); document.getElementById('activate').checked = true; document.getElementById('deactivate').checked = false; } function setOptions(options) { polygonControl.handler.setOptions(options); }
3、操作选项:
<input type="radio" name="type" value="none" id="deactivate" onclick="polygonControl.deactivate()" checked="checked" />deactivate <input type="radio" name="type" value="none" id="activate" onclick="polygonControl.activate()" />activate <select name="sides" onchange="setOptions({sides: parseInt(this.value)})"> <option value="3">triangle</option> <option value="4" selected="selected">square</option> <option value="5">pentagon</option> <option value="6">hexagon</option> <option value="40">circle</option> </select>
纸上得来终觉浅,绝知此事要躬行。