-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadkey.js
107 lines (94 loc) · 3.58 KB
/
quadkey.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
/**
* 初期化
*/
var ymap = new Y.Map("map", {
"configure": {
"dragging": true,
"singleClickPan": true,
"doubleClickZoom": true,
"continuousZoom": true,
"scrollWheelZoom": true
}
});
/**
* いろいろかくよ
* @class CanvasLayer
* @extends {Y.Layer}
*/
class CanvasLayer extends Y.Layer {
constructor(targetid, geohash_precision) {
super();
self.targetid_ = targetid;
self.canvas_ = null;
self.geohash_precision = parseInt(geohash_precision) || 4;
self.geohashArray = new Array();
}
drawLayer() {
var that = this;
if (self.canvas_) {
self.canvas_.remove();
self.geohashArray = new Array();
}
var elem = document.getElementById(self.targetid_)
var canvas = document.createElement("canvas");
canvas.style.position = "fixed";
canvas.width = elem.offsetWidth; // はめ込み先の幅
canvas.height = elem.offsetHeight; // はめ込み先の高さ
canvas.style.top = 0;
canvas.style.left = 0;
self.canvas_ = canvas;
var container = this.getMapContainer(); // canvas をはめ込む親要素を取得
if (!container || !container[0]) {
return;
}
container[0].appendChild(canvas); // canvas をはめ込む
var ymap = this.getMap();
var center = ymap.getCenter()
var geohash = Geohash.encode(center.Lat, center.Lon, self.geohash_precision);
var strokeGeohash = function (geohash) {
var bounds = Geohash.bounds(geohash);
var ne = that.fromLatLngToContainerPixel(new Y.LatLng(bounds.ne.lat, bounds.ne.lon));
var sw = that.fromLatLngToContainerPixel(new Y.LatLng(bounds.sw.lat, bounds.sw.lon));
// canvas に描画する
var ctx = self.canvas_.getContext('2d');
ctx.strokeStyle = "red";
ctx.strokeRect(sw.x, ne.y, ne.x - sw.x, sw.y - ne.y);
ctx.strokeText(geohash, sw.x + 5, ne.y + 15);
}
var limit = 500;
var fillGeohash = function (geohash) {
// 描画済みか確認
if (self.geohashArray.includes(geohash)) {
return;
}
// 一応リミット...
if (self.geohashArray.length > limit) {
return;
}
// 描画エリア内か確認
var bounds = Geohash.bounds(geohash);
var ymap = that.getMap();
var map_bounds = ymap.getBounds();
if (!(
map_bounds.containsLatLng(new Y.LatLng(bounds.ne.lat, bounds.ne.lon)) || // 北東
map_bounds.containsLatLng(new Y.LatLng(bounds.sw.lat, bounds.ne.lon)) || // 北西
map_bounds.containsLatLng(new Y.LatLng(bounds.sw.lat, bounds.sw.lon)) || // 南西
map_bounds.containsLatLng(new Y.LatLng(bounds.ne.lat, bounds.sw.lon)) // 南東
)) {
return;
}
// 描画してリストに追加
self.geohashArray.push(geohash);
strokeGeohash(geohash);
var neighbours = Geohash.neighbours(geohash);
for (var angle in neighbours) {
fillGeohash(neighbours[angle]);
}
}
fillGeohash(geohash);
}
}
var url = new URL(window.location);
var canvasLayer = new CanvasLayer('map', url.searchParams.get('geohash_length'));
ymap.addLayer(canvasLayer);
ymap.drawMap(new Y.LatLng(35.66572, 139.73100), url.searchParams.get('z') || 15, Y.LayerSetId.NORMAL);