<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">OpenLayers Live Measurement of Circle Radius</h1>
<div id="tags">drawing</div>
<p id="shortdesc">Draw a circle to see its live radius.</p>
<h2>
Radius: <span id="radius">xxx</span> <span id="einheit">Meter</span>
</h2>
<div id="map" class="smallmap"></div>
CSS
#radius {
color:red;
font-weight:bold;
}
JavaScript
var circles = new OpenLayers.Layer.Vector("Kreise");
//initialize a draw control
var my_polygonhandler = OpenLayers.Handler.RegularPolygon;
var polygonControl = new OpenLayers.Control.DrawFeature(circles,
my_polygonhandler, {
handlerOptions: {
sides: 40
}
});
//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]
});
var mittelpunkt = new OpenLayers.LonLat(-1.112207, 45.379580).transform(map.displayProjection, map.projection);
map.setCenter(mittelpunkt, 12);
map.addControl(polygonControl);
polygonControl.activate();
polygonControl.handler.callbacks.move = function (e) {
var linearRing = new OpenLayers.Geometry.LinearRing(e.components[0].components);
var geometry = new OpenLayers.Geometry.Polygon([linearRing]);
var polygonFeature = new OpenLayers.Feature.Vector(geometry, null);
var polybounds = polygonFeature.geometry.getBounds();
var minX = polybounds.left;
var minY = polybounds.bottom;
var maxX = polybounds.right;
var maxY = polybounds.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]);
var len = Math.round(radius.getLength()).toString();
var laenge;
if (len > 1000) {
laenge = len / 1000;
einheit = "km";
} else {
laenge = len;
einheit = "m";
}
document.getElementById("radius").innerHTML = laenge;
document.getElementById("einheit").innerHTML = einheit;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.