-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraticules.js
94 lines (80 loc) · 2.68 KB
/
Graticules.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
// Graticules for Polymaps: latitude/longitude lines
// Started as cut and paste from Grid.js in Polymaps,
// Nelson Minar <nelson@monkey.org> licensed as Polymaps itself.
var zero = {x: 0, y: 0};
po.graticules = function() {
var grid = {},
map,
g = po.svg("g");
g.setAttribute("class", "graticules");
function move(e) {
var p,
line = g.firstChild,
label = null,
size = map.size(),
nw = map.pointLocation(zero),
se = map.pointLocation(size),
step = Math.pow(2, 8 - Math.round(map.zoom())),
prec = 0;
if (map.zoom() == 9) { prec = 1; }
else if (map.zoom() == 10) { prec = 2; }
else if (map.zoom() == 11) { prec = 3; }
else if (map.zoom() > 11) { prec = 4; }
// Round to step.
nw.lat = Math.floor(nw.lat / step) * step;
nw.lon = Math.ceil(nw.lon / step) * step;
var orignw = { lat: nw.lat, lon: nw.lon };
// Longitude ticks.
for (var x; (x = map.locationPoint(nw).x) <= size.x; nw.lon += step) {
if (!line) line = g.appendChild(po.svg("line"));
line.setAttribute("x1", x);
line.setAttribute("x2", x);
line.setAttribute("y1", 0);
line.setAttribute("y2", size.y);
label = line.nextSibling;
if (!label) label = g.appendChild(po.svg("text"));
label.setAttribute("x", x + 2);
label.setAttribute("y", map.locationPoint(orignw).y + 12);
label.textContent = "" + nw.lon.toFixed(prec);
line = label.nextSibling;
}
// Latitude ticks.
for (var y; (y = map.locationPoint(nw).y) <= size.y; nw.lat -= step) {
if (!line) line = g.appendChild(po.svg("line"));
line.setAttribute("y1", y);
line.setAttribute("y2", y);
line.setAttribute("x1", 0);
line.setAttribute("x2", size.x);
label = line.nextSibling;
if (!label) label = g.appendChild(po.svg("text"));
label.setAttribute("x", map.locationPoint(orignw).x + 2);
label.setAttribute("y", y + 10);
if (y == map.locationPoint(orignw).y) {
label.textContent = ""; // don't show top latitude (collides with longitude)
} else {
label.textContent = "" + nw.lat.toFixed(prec);
}
line = label.nextSibling;
}
// Remove extra ticks.
while (line) {
var next = line.nextSibling;
g.removeChild(line);
line = next;
}
}
grid.map = function(x) {
if (!arguments.length) return map;
if (map) {
g.parentNode.removeChild(g);
map.off("move", move).off("resize", move);
}
if (map = x) {
map.on("move", move).on("resize", move);
map.container().appendChild(g);
map.dispatch({type: "move"});
}
return grid;
};
return grid;
};