OpenLayers live circle radius
HTML
<link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css">
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<link rel="stylesheet" href="http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/style.css">
<script src="http://web-mapping.com/test/RegularPolygon_thomas.js"></script>
<h1 id="title">Live Measurement of Circle Radius</h1>
<div id="tags">drawing</div>
<p id="shortdesc">Draw a circle to see its live radius.</p>
Radius: <span id="radius">xxx</span> <span id="einheit">Meter</span>
<div id="map" class="smallmap"></div>
CSS
#radius
{
color:red;
font-weight:bold;
}
JavaScript
//initialize two vector layers
var circles = new OpenLayers.Layer.Vector("Circles");
var radii = new OpenLayers.Layer.Vector("Radii");
//initialize a draw control
var my_polygonhandler=OpenLayers.Handler.RegularPolygon;
var polygonControl = new OpenLayers.Control.DrawFeature(circles,
my_polygonhandler, {
handlerOptions: {
sides: 40
}
});
console.log(polygonControl);
//initialize a map
var map = new OpenLayers.Map({
div: 'map',
projection: new OpenLayers.Projection('EPSG:900913'),
displayProjection: new OpenLayers.Projection('EPSG:4326'),
layers: [
new OpenLayers.Layer.OSM(),
circles,
radii]
});
if (!map.getCenter()) {
map.zoomToMaxExtent();
}
map.addControl(polygonControl);
polygonControl.activate();
circles.events.on({
'featureadded': function (e) {
var f = e.feature;
//calculate the min/max coordinates of a circle
var minX = f.geometry.bounds.left;
var minY = f.geometry.bounds.bottom;
var maxX = f.geometry.bounds.right;
var maxY = f.geometry.bounds.top;
//calculate the center coordinates
var startX = (minX + maxX) / 2;
var startY = (minY + maxY) / 2;
//make two points at center and at the edge
var startPoint = new OpenLayers.Geometry.Point(startX, startY);
var endPoint = new OpenLayers.Geometry.Point(maxX, startY);
var radius = new OpenLayers.Geometry.LineString([startPoint, endPoint]);
//calculate length. WARNING! The EPSG:900913 lengths are meaningless except around the equator. Either use a local coordinate system like UTM, or geodesic calculations.
var len = Math.round(radius.getLength()).toString();
//style the radius
var style = {
strokeColor: "#0500bd",
strokeWidth: 3
//,label: len
};
//add radius feature to radii layer
document.getElementById("radius").innerHTML=len;
var fea = new...