Give the ScaleLine control a geodesic option. Setting this to true will provide an accurate scale bar in Spherical Mercator maps. r=bartvde (closes #1890)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10110 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -61,6 +61,12 @@ OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* {DOMElement}
|
* {DOMElement}
|
||||||
*/
|
*/
|
||||||
eBottom:null,
|
eBottom:null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: geodesic
|
||||||
|
* {Boolean} Use geodesic measurement. Default is false.
|
||||||
|
*/
|
||||||
|
geodesic: false,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor: OpenLayers.Control.ScaleLine
|
* Constructor: OpenLayers.Control.ScaleLine
|
||||||
@@ -156,7 +162,14 @@ OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
var inches = OpenLayers.INCHES_PER_UNIT;
|
var inches = OpenLayers.INCHES_PER_UNIT;
|
||||||
|
|
||||||
// convert maxWidth to map units
|
// convert maxWidth to map units
|
||||||
var maxSizeData = this.maxWidth * res * inches[curMapUnits];
|
var maxSizeData = this.maxWidth * res * inches[curMapUnits];
|
||||||
|
var geodesicRatio = 1;
|
||||||
|
if(this.geodesic === true) {
|
||||||
|
var maxSizeGeodesic = this.getGeodesicLength(this.maxWidth);
|
||||||
|
var maxSizeKilometers = maxSizeData / inches["km"];
|
||||||
|
geodesicRatio = maxSizeGeodesic / maxSizeKilometers;
|
||||||
|
maxSizeData *= geodesicRatio;
|
||||||
|
}
|
||||||
|
|
||||||
// decide whether to use large or small scale units
|
// decide whether to use large or small scale units
|
||||||
var topUnits;
|
var topUnits;
|
||||||
@@ -182,8 +195,8 @@ OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
bottomMax = bottomRounded / inches[curMapUnits] * inches[bottomUnits];
|
bottomMax = bottomRounded / inches[curMapUnits] * inches[bottomUnits];
|
||||||
|
|
||||||
// and to pixel units
|
// and to pixel units
|
||||||
var topPx = topMax / res;
|
var topPx = topMax / res / geodesicRatio;
|
||||||
var bottomPx = bottomMax / res;
|
var bottomPx = bottomMax / res / geodesicRatio;
|
||||||
|
|
||||||
// now set the pixel widths
|
// now set the pixel widths
|
||||||
// and the values inside them
|
// and the values inside them
|
||||||
@@ -200,6 +213,26 @@ OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method: getGeodesicLength
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* pixels - {Number} the pixels to get the geodesic length in meters for.
|
||||||
|
*/
|
||||||
|
getGeodesicLength: function(pixels) {
|
||||||
|
var map = this.map;
|
||||||
|
var centerPx = map.getPixelFromLonLat(map.getCenter());
|
||||||
|
var bottom = map.getLonLatFromPixel(centerPx.add(0, -pixels / 2));
|
||||||
|
var top = map.getLonLatFromPixel(centerPx.add(0, pixels / 2));
|
||||||
|
var source = map.getProjectionObject();
|
||||||
|
var dest = new OpenLayers.Projection("EPSG:4326");
|
||||||
|
if(!source.equals(dest)) {
|
||||||
|
bottom.transform(source, dest);
|
||||||
|
top.transform(source, dest);
|
||||||
|
}
|
||||||
|
return OpenLayers.Util.distVincenty(bottom, top);
|
||||||
|
},
|
||||||
|
|
||||||
CLASS_NAME: "OpenLayers.Control.ScaleLine"
|
CLASS_NAME: "OpenLayers.Control.ScaleLine"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,160 @@
|
|||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="../../theme/default/style.css" type="text/css" />
|
||||||
|
<style type="text/css">
|
||||||
|
#controlToggle li {
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
#options {
|
||||||
|
position: relative;
|
||||||
|
width: 512px;
|
||||||
|
}
|
||||||
|
#output {
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* avoid pink tiles */
|
||||||
|
.olImageLoadError {
|
||||||
|
background-color: transparent !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script src="../../lib/OpenLayers.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
var map, measureControls;
|
||||||
|
function init(){
|
||||||
|
map = new OpenLayers.Map('map');
|
||||||
|
|
||||||
|
var wmsLayer = new OpenLayers.Layer.OSM();
|
||||||
|
|
||||||
|
map.addLayers([wmsLayer]);
|
||||||
|
map.setCenter(new OpenLayers.LonLat(0,0), 5);
|
||||||
|
map.addControl(new OpenLayers.Control.LayerSwitcher());
|
||||||
|
map.addControl(new OpenLayers.Control.MousePosition());
|
||||||
|
map.addControl(new OpenLayers.Control.ScaleLine({geodesic: true}))
|
||||||
|
|
||||||
|
// style the sketch fancy
|
||||||
|
var sketchSymbolizers = {
|
||||||
|
"Point": {
|
||||||
|
pointRadius: 4,
|
||||||
|
graphicName: "square",
|
||||||
|
fillColor: "white",
|
||||||
|
fillOpacity: 1,
|
||||||
|
strokeWidth: 1,
|
||||||
|
strokeOpacity: 1,
|
||||||
|
strokeColor: "#333333"
|
||||||
|
},
|
||||||
|
"Line": {
|
||||||
|
strokeWidth: 3,
|
||||||
|
strokeOpacity: 1,
|
||||||
|
strokeColor: "#666666",
|
||||||
|
strokeDashstyle: "dash"
|
||||||
|
},
|
||||||
|
"Polygon": {
|
||||||
|
strokeWidth: 2,
|
||||||
|
strokeOpacity: 1,
|
||||||
|
strokeColor: "#666666",
|
||||||
|
fillColor: "white",
|
||||||
|
fillOpacity: 0.3
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var style = new OpenLayers.Style();
|
||||||
|
style.addRules([
|
||||||
|
new OpenLayers.Rule({symbolizer: sketchSymbolizers})
|
||||||
|
]);
|
||||||
|
var styleMap = new OpenLayers.StyleMap({"default": style});
|
||||||
|
|
||||||
|
measureControls = {
|
||||||
|
line: new OpenLayers.Control.Measure(
|
||||||
|
OpenLayers.Handler.Path, {
|
||||||
|
geodesic: true,
|
||||||
|
persist: true,
|
||||||
|
handlerOptions: {
|
||||||
|
layerOptions: {styleMap: styleMap}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
polygon: new OpenLayers.Control.Measure(
|
||||||
|
OpenLayers.Handler.Polygon, {
|
||||||
|
geodesic: true,
|
||||||
|
persist: true,
|
||||||
|
handlerOptions: {
|
||||||
|
layerOptions: {styleMap: styleMap}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
var control;
|
||||||
|
for(var key in measureControls) {
|
||||||
|
control = measureControls[key];
|
||||||
|
control.events.on({
|
||||||
|
"measure": handleMeasurements,
|
||||||
|
"measurepartial": handleMeasurements
|
||||||
|
});
|
||||||
|
map.addControl(control);
|
||||||
|
}
|
||||||
|
|
||||||
|
map.setCenter(new OpenLayers.LonLat(0, 0), 3);
|
||||||
|
|
||||||
|
document.getElementById('noneToggle').checked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMeasurements(event) {
|
||||||
|
var geometry = event.geometry;
|
||||||
|
var units = event.units;
|
||||||
|
var order = event.order;
|
||||||
|
var measure = event.measure;
|
||||||
|
var element = document.getElementById('output');
|
||||||
|
var out = "";
|
||||||
|
if(order == 1) {
|
||||||
|
out += "measure: " + measure.toFixed(3) + " " + units;
|
||||||
|
} else {
|
||||||
|
out += "measure: " + measure.toFixed(3) + " " + units + "<sup>2</" + "sup>";
|
||||||
|
}
|
||||||
|
element.innerHTML = out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleControl(element) {
|
||||||
|
for(key in measureControls) {
|
||||||
|
var control = measureControls[key];
|
||||||
|
if(element.value == key && element.checked) {
|
||||||
|
control.activate();
|
||||||
|
} else {
|
||||||
|
control.deactivate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body onload="init()">
|
||||||
|
<h1 id="title">OpenLayers Geodesic Measurement & ScaleLine</h1>
|
||||||
|
<p id="shortdesc">
|
||||||
|
Tests geodesic measurement of distances and areas against a geodesic ScaleLine.
|
||||||
|
</p>
|
||||||
|
<div id="map" style="width: 512px; height: 300px;"></div>
|
||||||
|
<div id="options">
|
||||||
|
<div id="output">
|
||||||
|
</div>
|
||||||
|
<ul id="controlToggle">
|
||||||
|
<li>
|
||||||
|
<input type="radio" name="type" value="none" id="noneToggle"
|
||||||
|
onclick="toggleControl(this);" checked="checked" />
|
||||||
|
<label for="noneToggle">navigate</label>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<input type="radio" name="type" value="line" id="lineToggle" onclick="toggleControl(this);" />
|
||||||
|
<label for="lineToggle">measure distance</label>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<input type="radio" name="type" value="polygon" id="polygonToggle" onclick="toggleControl(this);" />
|
||||||
|
<label for="polygonToggle">measure area</label>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<p>Zoom in so the ScaleLine shows units in the range of 10-100 km. Measure
|
||||||
|
the length of the ScaleLine. The result should be approximately the same
|
||||||
|
as the distance printed on the ScaleLine.</p>
|
||||||
|
<p>Zoom out so the ScaleLine shows units in the range of 100-500 km. Drag
|
||||||
|
the map to the South or North and see how the ScaleLine length changes.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user