-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
boxes.js
103 lines (88 loc) · 3.2 KB
/
boxes.js
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
var CREATE_NEW_BOXES = false;
var boxesLayer;
function addBoxesToMap() {
/* inspired by http://dev.openlayers.org/examples/drag-feature.html
* and http://dev.openlayers.org/examples/setextent.html
*/
// allow testing of specific renderers via "?renderer=Canvas", etc
var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
boxesLayer = new OpenLayers.Layer.Boxes(translate("Offline Map Areas"));
//new OpenLayers.Layer.Vector(translate("Offline Map Areas"), {
// renderers: renderer
//});
// var mouseControl = new OpenLayers.Control.MousePosition();
//map.addControl(mouseControl);
// var dragControl = new OpenLayers.Control.DragFeature(boxesLayer);
// map.addControl(dragControl);
// dragControl.activate();
// mouseControl.activate();
map.addLayer(boxesLayer);
boxesLayer.setVisibility(CREATE_NEW_BOXES);
}
var lastClickLonLat = null;
var boundingBoxes = [];
function notifyBoxesAboutClick(lonlat2) {
if (!CREATE_NEW_BOXES) {
return;
}
if (lastClickLonLat == null) {
lastClickLonLat = lonlat2;
return;
}
var lonlat1 = lastClickLonLat;
lastClickLonLat = null;
var lon1 = Math.min(lonlat1.lon, lonlat2.lon);
var lon2 = Math.max(lonlat1.lon, lonlat2.lon);
if (Math.abs(lon1 - lon2) > 180) {
var tmp = lon1;
lon1 = lon2;
lon2 = tmp;
}
var lat1 = Math.min(lonlat1.lat, lonlat2.lat);
var lat2 = Math.max(lonlat1.lat, lonlat2.lat);
var bbox = {
north: lat2,
south: lat1,
west: lon1,
east: lon2,
};
addBoundingBox(bbox);
}
function addBoundingBox(bbox) {
log.log("add bbox", bbox);
if (boundingBoxes.indexOf(bbox) == -1) {
boundingBoxes.push(bbox);
}
if (Math.abs(bbox.west - bbox.east) > 180) {
bbox.east += 360;
}
var point1 = lonLatToMarkerPosition({lon: bbox.west, lat: bbox.south});
var point2 = lonLatToMarkerPosition({lon: bbox.east, lat: bbox.north});
var bounds = new OpenLayers.Bounds(point1.lon, point1.lat, point2.lon, point2.lat);
var box = new OpenLayers.Marker.Box(bounds, "#cc1111", 3); // http://dev.openlayers.org/docs/files/OpenLayers/Marker/Box-js.html
boxesLayer.addMarker(box);
box.events.register("click", box, function (e) { // from http://dev.openlayers.org/examples/boxes.html
if (lastClickLonLat == null) {
// remove marker
boxesLayer.removeMarker(this);
this.destroy();
boundingBoxes.splice(boundingBoxes.indexOf(bbox), 1);
setConfigurationInURL();
} else {
// create marker with overlap
var lonlat = getLonLatFromPixel({x: e.clientX, y: e.clientY});
notifyBoxesAboutClick(lonlat);
}
});
setConfigurationInURL();
box.div.style.background = "rgba(1, 1, 1, 0.1)";
}
function redrawAllBoundingBoxes() {
while (boxesLayer.markers.length > 0) {
var marker = boxesLayer.markers[0];
marker.destroy();
boxesLayer.removeMarker(marker);
}
boundingBoxes.forEach(addBoundingBox);
}