-
Notifications
You must be signed in to change notification settings - Fork 3
/
grat.js
286 lines (254 loc) · 8.06 KB
/
grat.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Graticule for google.maps v3
//
// Adapted from Bill Chadwick 2006 http://www.bdcc.co.uk/Gmaps/BdccGmapBits.htm
// which is free for any use.
//
// This work is licensed under the Creative Commons Attribution 3.0 Unported
// License. To view a copy of this license, visit
// http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
// Commons, 171 Second Street, Suite 300, San Francisco, California, 94105,
// USA.
//
// Matthew Shen 2011
//
// Reworked some more by Bill Chadwick ...
//
var Graticule = (function() {
function _(map, sexagesimal) {
// default to decimal intervals
this.sex_ = sexagesimal || false;
this.set('container', document.createElement('DIV'));
this.show();
this.setMap(map);
}
_.prototype = new google.maps.OverlayView();
_.prototype.addDiv = function(div) {
this.get('container').appendChild(div);
},
_.prototype.decToLonSex = function(d) {
var degs = Math.floor(Math.abs(d));
var mins = ((Math.abs(d) - degs) * 60.0).toFixed(2);
if (mins == "60.00") { degs += 1.0; mins = "0.00"; }
return [degs, (d>0)?"E":"W", mins].join('');
};
_.prototype.decToLatSex = function(d) {
var degs = Math.floor(Math.abs(d));
var mins = ((Math.abs(d) - degs) * 60.0).toFixed(2);
if (mins == "60.00") { degs += 1.0; mins = "0.00"; }
return [degs, (d>0)?"N":"S", mins].join('');
};
_.prototype.onAdd = function() {
var self = this;
this.getPanes().mapPane.appendChild(this.get('container'));
function redraw() {
self.draw();
}
this.idleHandler_ = google.maps.event.addListener(this.getMap(), 'idle', redraw);
function changeColor() {
self.draw();
}
changeColor();
this.typeHandler_ = google.maps.event.addListener(this.getMap(), 'maptypeid_changed', changeColor);
};
_.prototype.clear = function() {
var container = this.get('container');
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
};
_.prototype.onRemove = function() {
this.get('container').parentNode.removeChild(this.get('container'));
this.set('container', null);
google.maps.event.removeListener(this.idleHandler_);
google.maps.event.removeListener(this.typeHandler_);
};
_.prototype.show = function() {
this.get('container').style.visibility = 'visible';
};
_.prototype.hide = function() {
this.get('container').style.visibility = 'hidden';
};
function _bestTextColor(overlay) {
var type = overlay.getMap().getMapTypeId();
var GMM = google.maps.MapTypeId;
if (type === GMM.HYBRID) return '#fff';
if (type === GMM.ROADMAP) return '#000';
if (type === GMM.SATELLITE) return '#fff';
if (type === GMM.TERRAIN) return '#000';
var mt = overlay.getMap().mapTypes[type];
return (mt.textColor) ? mt.textColor : '#fff'; //ported legacy V2 map layers may have a textColor property
};
function gridPrecision(dDeg) {
if (dDeg < 0.01) return 3;
if (dDeg < 0.1) return 2;
if (dDeg < 1) return 1;
return 0;
}
function leThenReturn(x, l, d) {
for (var i = 0; i < l.length; i += 1) {
if (x <= l[i]) {
return l[i];
}
}
return d;
}
var numLines = 10;
var decmins = [
0.06, // 0.001 degrees
0.12, // 0.002 degrees
0.3, // 0.005 degrees
0.6, // 0.01 degrees
1.2, // 0.02 degrees
3, // 0.05 degrees
6, // 0.1 degrees
12, // 0.2 degrees
30, // 0.5
60, // 1
60 * 2,
60 * 5,
60 * 10,
60 * 20,
60 * 30,
];
var sexmins = [
0.01, // minutes
0.02,
0.05,
0.1,
0.2,
0.5,
1.0,
3, // 0.05 degrees
6, // 0.1 degrees
12, // 0.2 degrees
30, // 0.5
60, // 1
60 * 2,
60 * 5,
60 * 10,
60 * 20,
60 * 30,
];
function mins_list(overlay) {
if (overlay.sex_) return sexmins;
return decmins;
}
function latLngToPixel(overlay, lat, lng) {
return overlay.getProjection().fromLatLngToDivPixel(
new google.maps.LatLng(lat, lng));
};
// calculate rounded graticule interval in decimals of degrees for supplied
// lat/lon span return is in minutes
function gridInterval(dDeg, mins) {
return leThenReturn(Math.ceil(dDeg / numLines * 6000) / 100, mins,
60 * 45) / 60;
}
function npx(n) {
return n.toString() + 'px';
}
function makeLabel(color, x, y, text) {
var d = document.createElement('DIV');
var s = d.style;
s.position = 'absolute';
s.left = npx(x);
s.top = npx(y);
s.color = color;
s.width = '3em';
s.fontSize = '1.0em';
s.whiteSpace = 'nowrap';
d.innerHTML = text;
return d;
};
function createLine(x, y, w, h, color) {
var d = document.createElement('DIV');
var s = d.style;
s.position = 'absolute';
s.overflow = 'hidden';
s.backgroundColor = color;
s.opacity = 0.3;
var s = d.style;
s.left = npx(x);
s.top = npx(y);
s.width = npx(w);
s.height = npx(h);
return d;
};
var span = 50000;
function meridian(px, color) {
return createLine(px, -span, 1, 2 * span, color);
}
function parallel(py, color) {
return createLine(-span, py, 2 * span, 1, color);
}
function eqE(a, b, e) {
if (!e) {
e = Math.pow(10, -6);
}
if (Math.abs(a - b) < e) {
return true;
}
return false;
}
// Redraw the graticule based on the current projection and zoom level
_.prototype.draw = function() {
var color = _bestTextColor(this);
this.clear();
if (this.get('container').style.visibility != 'visible') {
return;
}
// determine graticule interval
var bnds = this.getMap().getBounds();
if (!bnds) {
// The map is not ready yet.
return;
}
var sw = bnds.getSouthWest(),
ne = bnds.getNorthEast();
var l = sw.lng(),
b = sw.lat(),
r = ne.lng(),
t = ne.lat();
if (l == r) { l = -180.0; r = 180.0; }
if (t == b) { b = -90.0; t = 90.0; }
// grid interval in degrees
var mins = mins_list(this);
var dLat = gridInterval(t - b, mins);
var dLng = gridInterval(r > l ? r - l : ((180 - l) + (r + 180)), mins);
// round iteration limits to the computed grid interval
l = Math.floor(l / dLng) * dLng;
b = Math.floor(b / dLat) * dLat;
t = Math.ceil(t / dLat) * dLat;
r = Math.ceil(r / dLng) * dLng;
if (r == l) l += dLng;
if (r < l) r += 360.0;
// lngs
var crosslng = l + 2 * dLng;
// labels on second column to avoid peripheral controls
var y = latLngToPixel(this, b + 2 * dLat, l).y + 2;
// lo<r to skip printing 180/-180
for (var lo = l; lo < r; lo += dLng) {
if (lo > 180.0) {
r -= 360.0;
lo -= 360.0;
}
var px = latLngToPixel(this, b, lo).x;
this.addDiv(meridian(px, color));
var atcross = eqE(lo, crosslng);
this.addDiv(makeLabel(color,
px + (atcross ? 17 : 3), y - (atcross ? 3 : 0),
(this.sex_ ? this.decToLonSex(lo) : lo.toFixed(gridPrecision(dLng)))));
}
// lats
var crosslat = b + 2 * dLat;
// labels on second row to avoid controls
var x = latLngToPixel(this, b, l + 2 * dLng).x + 3;
for (; b <= t; b += dLat) {
var py = latLngToPixel(this, b, l).y;
this.addDiv(parallel(py, color));
this.addDiv(makeLabel(color,
x, py + (eqE(b, crosslat) ? 7 : 2),
(this.sex_ ? this.decToLatSex(b) : b.toFixed(gridPrecision(dLat)))));
}
};
return _;
})();