1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
<!DOCTYPE html>
<html>
<head>
<title>Get Bounding Box</title>
<script type="text/javascript" src="../../modestmaps.js"></script>
<script type="text/javascript">
function BoundingBox(map) {
this.map = map;
var theBox = this;
this.getMousePoint = function(e) {
// start with just the mouse (x, y)
var point = new com.modestmaps.Point(e.clientX, e.clientY);
// correct for scrolled document
point.x += document.body.scrollLeft + document.documentElement.scrollLeft;
point.y += document.body.scrollTop + document.documentElement.scrollTop;
// correct for nested offsets in DOM
for(var node = this.map.parent; node; node = node.offsetParent) {
point.x -= node.offsetLeft;
point.y -= node.offsetTop;
}
return point;
};
var boxDiv = document.createElement('div');
boxDiv.id = map.parent.id+'-boundingBox';
boxDiv.width = map.dimensions.x;
boxDiv.height = map.dimensions.y;
boxDiv.style.margin = '0';
boxDiv.style.padding = '0';
boxDiv.style.position = 'absolute';
boxDiv.style.top = '0px';
boxDiv.style.left = '0px';
boxDiv.style.width = map.dimensions.x+'px';
boxDiv.style.height = map.dimensions.y+'px';
map.parent.appendChild(boxDiv);
var box = document.createElement('div');
box.id = map.parent.id+'-boundingBox-box';
box.width = map.dimensions.x;
box.height = map.dimensions.y;
box.style.margin = '0';
box.style.padding = '0';
box.style.outlineWidth = '1000px';
box.style.outlineColor = 'rgba(0,0,0,0.2)';
box.style.outlineStyle = 'solid';
box.style.position = 'absolute';
box.style.display = 'none';
box.style.top = '0px';
box.style.left = '0px';
box.style.width = '0px';
box.style.height = '0px';
boxDiv.appendChild(box);
// TODO: respond to resize
var mouseDownPoint = null;
this.mouseDown = function(e) {
if (e.shiftKey) {
mouseDownPoint = theBox.getMousePoint(e);
box.style.width = '0px';
box.style.height = '0px';
box.style.left = mouseDownPoint.x + 'px';
box.style.top = mouseDownPoint.y + 'px';
com.modestmaps.addEvent(map.parent, 'mousemove', theBox.mouseMove);
com.modestmaps.addEvent(map.parent, 'mouseup', theBox.mouseUp);
map.parent.style.cursor = 'crosshair';
return com.modestmaps.cancelEvent(e);
}
};
this.mouseMove = function(e) {
var point = theBox.getMousePoint(e);
box.style.display = 'block';
if (point.x < mouseDownPoint.x) {
box.style.left = point.x + 'px';
}
else {
box.style.left = mouseDownPoint.x + 'px';
}
box.style.width = Math.abs(point.x - mouseDownPoint.x) + 'px';
if (point.y < mouseDownPoint.y) {
box.style.top = point.y + 'px';
}
else {
box.style.top = mouseDownPoint.y + 'px';
}
box.style.height = Math.abs(point.y - mouseDownPoint.y) + 'px';
theBox.updateInfo();
return com.modestmaps.cancelEvent(e);
};
this.updateInfo = function() {
if (box.style.display != 'none') {
var p1 = new com.modestmaps.Point(box.offsetLeft, box.offsetTop);
var p2 = new com.modestmaps.Point(box.offsetLeft+box.offsetWidth, box.offsetTop+box.offsetHeight);
//window.console.log(p1,p2);
var l1 = map.pointLocation(p1);
var l2 = map.pointLocation(p2);
//window.console.log(l1,l2);
var northWest = new com.modestmaps.Location(Math.max(l1.lat,l2.lat), Math.min(l1.lon, l2.lon));
var southEast = new com.modestmaps.Location(Math.min(l1.lat,l2.lat), Math.max(l1.lon, l2.lon));
document.getElementById('info').innerHTML = "N,W,S,E: <tt>" + [ northWest.lat.toFixed(6), northWest.lon.toFixed(6), southEast.lat.toFixed(6), southEast.lon.toFixed(6) ].join(', ') + "</tt>";
document.getElementById('info').innerHTML += "<br>S,W,N,E: <tt>" + [ southEast.lat.toFixed(6), northWest.lon.toFixed(6), northWest.lat.toFixed(6), southEast.lon.toFixed(6) ].join(', ') + "</tt>";
}
};
this.mouseUp = function(e) {
theBox.updateInfo();
com.modestmaps.removeEvent(map.parent, 'mousemove', theBox.mouseMove);
com.modestmaps.removeEvent(map.parent, 'mouseup', theBox.mouseUp);
map.parent.style.cursor = 'auto';
return com.modestmaps.cancelEvent(e);
};
com.modestmaps.addEvent(boxDiv, 'mousedown', this.mouseDown);
}
var map, boundingBox;
function initMap() {
var container = document.getElementById('container');
map = new com.modestmaps.Map('map',
new com.modestmaps.TemplatedLayer('http://tile.openstreetmap.org/{Z}/{X}/{Y}.png'),
new com.modestmaps.Point(container.offsetWidth, container.offsetHeight));
map.setCenterZoom(new com.modestmaps.Location(0, 0), 1);
boundingBox = new BoundingBox(map);
window.onresize = function() {
map.setSize(container.offsetWidth, container.offsetHeight);
};
function onMapChange() {
boundingBox.updateInfo();
}
map.addCallback('panned', onMapChange);
map.addCallback('resized', onMapChange);
map.addCallback('zoomed', onMapChange);
map.addCallback('extentset', onMapChange);
map.addCallback('centered', onMapChange);
}
</script>
<style type="text/css">
body {
background: #fff;
color: #000;
font-family: sans-serif;
margin: 0;
padding: 20px;
border: 0;
}
div#container {
width: 100%;
height: 512px;
}
p#credit {
font-size: 80%;
color: #444;
text-align: center;
margin-top: 30px;
}
</style>
</head>
<body onload="initMap()">
<h1>Get Bounding Box</h1>
<p id="info">Shift-click and drag to draw a bounding box.<br> </p>
<div id="container">
<div id="map">
</div>
</div>
<p>Click and drag to pan, mouse-wheel to zoom. Shift-click and drag to draw a bounding box.</p>
<p id="credit">Powered by <a href="http://modestmaps.com">Modest Maps JS</a>. Map imagery <a href="http://wiki.openstreetmap.org/wiki/Licence">CC-BY-SA OpenStreetMap.org contributors</a>, used with reference to the <a href="http://wiki.openstreetmap.org/wiki/Tile_usage_policy">OSM tile usage policy</a>.</p>
</body>
</html>
|