-
Notifications
You must be signed in to change notification settings - Fork 0
/
geohash.js
106 lines (94 loc) · 3.34 KB
/
geohash.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
var ymap = new Y.Map("map", {
"configure": {
"dragging": true,
"singleClickPan": false,
"doubleClickZoom": true,
"continuousZoom": true,
"scrollWheelZoom": true
}
});
var url = new URL(window.location);
if (!url.searchParams.get('geohash_length') || !url.searchParams.get('geohash_length').match(/[0-9]+/)) {
window.location.href = 'geohash.html?geohash_length=5';
}
function updateHistory (p) {
// TODO : 5秒待って更新
history.replaceState('', '', p);
}
document.getElementById('geohash').addEventListener('change', function () {
var center = Geohash.decode(this.value);
ymap.drawMap(new Y.LatLng(center.lat, center.lon));
});
document.getElementById('geohash_length').addEventListener('change', function () {
window.location.href = createUrl(this.value);
});
ymap.bind('moveend', function () {
updateHistory(createUrl());
var url = new URL(window.location);
document.getElementById('geohash').value = url.searchParams.get('geohash');
document.getElementById('geohash').size = url.searchParams.get('geohash').length;
document.getElementById('geohash_length').value = url.searchParams.get('geohash_length');
document.getElementById('geohash_length').size = url.searchParams.get('geohash_length').length;
document.getElementById('position').value = url.searchParams.get('p');
document.getElementById('position').size = url.searchParams.get('p').length+5;
});
var alias = {};
var is_num = true;
var max_num = 0;
if (url.searchParams.get('alias')) {
var _alias = url.searchParams.get('alias').split(',').map(p=>{return p.split(':')});
for (var i=0; i<_alias.length; i++) {
alias[_alias[i][0]] = _alias[i][1];
}
// エイリアス設定があれば表示
var max_num = 0;
var is_num = new RegExp(/^[+-]?[0-9]+$/);
for (var a in alias) {
if (!is_num.test(alias[a])) {
is_num = false;
} else {
if (max_num < alias[a]) {
max_num = alias[a] * 1;
}
}
}
}
ymap.addLayer(new GeohashLayer('map', url.searchParams.get('geohash_length'), { alias : alias, is_num : is_num, max_num : max_num}));
var p = parseP(url.searchParams.get('p'));
ymap.drawMap(new Y.LatLng(p.Lat, p.Lon) , p.zoom , Y.LayerSetId.NORMAL);
document.getElementById('map').addEventListener ('ongeohashlimit', () => {
console.dir('fire limit');
location.href = createUrl(url.searchParams.get('geohash_length')*1-1);
});
window.addEventListener('resize', () => {
ymap.updateSize();
});
function createUrl(geohash_length) {
if (!geohash_length) {
geohash_length = url.searchParams.get('geohash_length');
}
var ll = ymap.getCenter();
var latlon = '@'+ll.Lat+','+ll.Lon
var alias = '';
if (url.searchParams.get('alias')) {
alias = '&alias=' + url.searchParams.get('alias');
}
return 'geohash.html?' +
'geohash_length=' + geohash_length +
'&geohash=' + Geohash.encode(ll.Lat, ll.Lon, url.searchParams.get('geohash_length')) +
'&p=' + latlon + ',' + ymap.zoom + 'z' +
alias;
;
}
function parseP (p) {
p = p + "";
var m = p.match(/@([0-9.]+),([0-9.]+),([0-9]+)z/);
if (m === null) {
m = {};
}
return {
Lat : m[1] || 35.171962,
Lon : m[2] || 136.8817322,
zoom : m[3] || 13
}
}