-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_check.js
358 lines (306 loc) · 12.2 KB
/
map_check.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
if (MapCheckApl || mapCheckApl)
throw "map_check.js already loaded";
// アプリケーションクラス定義
var MapCheckApl = Class.create();
MapCheckApl.prototype = {
initialize : function() {
// ここの初期値はHTMLのデフォルト値で上書きされる
this.bounds_ = $A([0,0,0,0]);
this.mat_ = $A([0,0]);
this.imageDir_ = "images";
var myOptions = {
zoom: 12, // 仮
center: new google.maps.LatLng(0,0),
mapTypeId: google.maps.MapTypeId.ROADMAP,
// mapTypeId: google.maps.MapTypeId.SATELLITE,
scaleControl: true
};
this.mainmap_ = new google.maps.Map(
document.getElementById("map_canvas"), myOptions);
this.tileImages_ = null;
},
noOverlay : function() {
return (this.mat_[0] == 0 || this.mat_[1] == 0
|| this.bounds_[0] - this.bounds_[2] == 0
|| this.bounds_[1] - this.bounds_[3] == 0);
},
overlayImages : function() {
var res = $('inputLatLng1').fire('my:input');
if (res.myError) {
alert(res.myError);
return;
}
res = $('inputMatRow1').fire('my:input');
if (res.myError) {
alert(res.myError);
return;
}
res = $('inputMatCol1').fire('my:input');
if (res.myError) {
alert(res.myError);
return;
}
$('inputInputDir1').fire('my:input');
if (this.noOverlay()) return;
if (this.tileImages_) this.tileImages_.clearOverlays();
var bounds = new google.maps.LatLngBounds(
// sw latlng
new google.maps.LatLng(this.bounds_[2], this.bounds_[1]),
// ne latlng
new google.maps.LatLng(this.bounds_[0], this.bounds_[3]));
this.tileImages_ = new this.TileImages(bounds, this.mat_, this.mainmap_);
this.tileImages_.loadAndOverlayImages(this.imageDir_);
// 最初のパン位置は全体の中心
this.mainmap_.panTo(bounds.getCenter());
},
inputLatLng : function(event) {
function isFloat(v) {
return /^[\+\-]?[0-9]+(\.[0-9]+)?$/.test(v);
};
var v = event.element().value;
var ll = $A(v.split(/, */));
try {
// check
if (ll.length != 4) throw "不正な緯度経度の入力";
ll.each(function(item) {
if (!isFloat(item)) throw "不正な緯度経度の入力"; });
this.bounds_ = $A(ll);
} catch (e) {
event.myError = e;
return 0;
}
return 1;
},
inputMatRow : function(event) {
var v = event.element().value;
try {
if (!/^[0-9]+$/.test(v)) throw "不正な分割値の入力";
} catch (e) {
event.myError = e;
return 0;
}
this.mat_[1] = v;
return 1;
},
inputMatCol : function(event) {
var v = event.element().value;
try {
if (!/^[0-9]+$/.test(v)) throw "不正な分割値の入力";
} catch (e) {
event.myError = e;
return 0;
}
this.mat_[0] = v;
return 1;
},
inputInputDir : function(event) {
// TODO: 不正な入力のチェック(優先度は低い)
// パスデリミタの正規化をして取り込む
var v = event.element().value.replace(/\\/g, "/");
// フルパスと思われるものは、"file://"をつける
if (v.match(/^[a-z]\:/i)) v = "file://" + v;
this.imageDir_ = v;
}
};
// タイル画像管理クラス
MapCheckApl.prototype.TileImages = Class.create();
MapCheckApl.prototype.TileImages.prototype = {
initialize : function(bounds, mat, mainmap) {
this.divImages_ = null;
this.overlays_ = null;
this.bounds_ = bounds;
this.mat_ = mat;
this.mainmap_ = mainmap;
},
loadAndOverlayImages : function(root) {
this.divImages_ = $A();
var all = this.mat_[0]*this.mat_[1];
for (var i = 0; i < all-1; ++i) {
var img = new Image();
img.src = this.getImageSrc(root, i);
this.divImages_.push(img);
}
// 最後の画像の読み込みにonloadを入れる。
// ただしこれがちゃんと一番最後に読み込みが終わるかは怪しい
// それともそれぞれにonloadつけたほうがいいのか??
// ただし、昔のコードではそれぞれにつけても1度しか呼ばれないなど
// 問題があった。
var img = new Image();
this.divImages_.push(img);
img.onload = this.onloadImages.bind(this);
img.src = this.getImageSrc(root, all-1);
},
getImageSrc : function(root, i) {
var fname = i.toString();
// ゼロサプレス。もっといいやり方ありそう
if (fname.length > 5) return fname + ".png";
var h = "";
$R(0, 5 - fname.length, true).each(function() {h += '0';});
// フォルダ名とつなげる。もっといいやり方ありそう
var r = root;
if (root.charAt(root.length-1) != "/") r += "/";
return r + h + fname + ".png";
},
onloadImages : function() {
try {
// まずタイル画像の緯度経度テーブルを作る
this.makeLatLngTable();
// googleマップに貼り付け
this.overlayImages();
} catch (e) {
alert(e);
this.clearOverlays();
return;
}
},
inputImageSizes : function(num, s_func) {
var out = $A();
for (var i = 0; i < num; i++) {
var v = s_func(i);
if (!v) { throw "画像読み込みエラー!"; }
out.push(v);
}
return out;
},
makeLatLngTable : function() {
// 画像サイズ取得
var horizontalSizes =
this.inputImageSizes(this.mat_[0],
function(i) { return this.divImages_[i].naturalWidth; }.bind(this));
var verticalSizes =
this.inputImageSizes(this.mat_[1],
function(i) { return this.divImages_[i*this.mat_[0]].naturalHeight; }.bind(this));
// サイズからタイル画像の緯度経度を求める
var proj = this.mainmap_.getProjection();
// 倍率を求める
var ne = this.bounds_.getNorthEast();
var sw = this.bounds_.getSouthWest();
var ne_p = proj.fromLatLngToPoint(ne);
var sw_p = proj.fromLatLngToPoint(sw);
var mag_x = this.calcMag(horizontalSizes, ne_p.x - sw_p.x);
var mag_y = this.calcMag(verticalSizes, sw_p.y - ne_p.y);
// タイル画像の緯度経度をセットする
this.tileLatLng_ = $A();
var pos = [0,0];
var spos = proj.fromLatLngToPoint(new google.maps.LatLng(ne.lat(), sw.lng()));
for (var j = 0; j < this.mat_[1]; j++) {
var size = null;
for (var i = 0; i < this.mat_[0]; i++) {
var img = this.divImages_[i + j*this.mat_[0]];
size = [img.naturalWidth, img.naturalHeight];
this.tileLatLng_
.push(new google.maps.LatLngBounds(
// sw latlng
proj.fromPointToLatLng(
new google.maps.Point(
spos.x + pos[0]*mag_x,
spos.y + (pos[1]+size[1])*mag_y)),
// ne latlng
proj.fromPointToLatLng(
new google.maps.Point(
spos.x + (pos[0]+size[0])*mag_x,
spos.y + pos[1]*mag_y))));
pos[0] += size[0];
}
pos[0] = 0;
pos[1] += size[1];
}
},
overlayImages : function() {
this.overlays_ = $A();
for (var i = 0; i < this.tileLatLng_.length; i++) {
this.overlays_.push(new this.MyOverlay(
this.mainmap_,
this.divImages_[i],
this.tileLatLng_[i]));
}
},
calcMag : function(sizes1, sz2) {
var sz1 = 0;
sizes1.each(function(item) { sz1 += item; });
return sz2 / sz1;
},
clearOverlays : function() {
if (this.overlays_ && this.overlays_.length > 0) {
this.overlays_.each(function(item) { item.setMap(null); });
}
this.overlays_ = null;
this.divImages_ = null;
this.tileLatLng_ = null;
}
};
// オーバーレイ用のクラス
// googleマップのサンプルを元に作っている
MapCheckApl.prototype.TileImages.prototype.MyOverlay = Class.create();
Object.extend(MapCheckApl.prototype.TileImages.prototype.MyOverlay.prototype,
google.maps.OverlayView.prototype);
Object.extend(
MapCheckApl.prototype.TileImages.prototype.MyOverlay.prototype, {
// 先頭3つの引数は仮
initialize : function(map, img, bounds) {
this.img_ = img;
this.bounds_ = bounds;
this.setMap(map);
},
onAdd : function() {
// Create the DIV and set some basic attributes.
var div = document.createElement('DIV');
div.style.borderStyle = "none";
div.style.borderWidth = "0px";
div.style.position = "absolute";
// Create an IMG element and attach it to the DIV.
var img = document.createElement("img");
img.src = this.img_.src;
img.style.width = "100%";
img.style.height = "100%";
img.style.MozOpacity = 0.4;
div.appendChild(img);
this.div_ = div;
// We add an overlay to a map via one of the map's panes.
// We'll add this overlay to the overlayImage pane.
var panes = this.getPanes();
panes.overlayImage.appendChild(div);
},
draw : function() {
// Size and position the overlay. We use a southwest and northeast
// position of the overlay to peg it to the correct position and size.
// We need to retrieve the projection from this overlay to do this.
var overlayProjection = this.getProjection();
var bounds = this.bounds_;
var sw, ne;
var div = this.div_;
// Retrieve the southwest and northeast coordinates of this overlay
// in latlngs and convert them to pixels coordinates.
// We'll use these coordinates to resize the DIV.
sw = overlayProjection.fromLatLngToDivPixel(bounds.getSouthWest());
ne = overlayProjection.fromLatLngToDivPixel(bounds.getNorthEast());
// Resize the image's DIV to fit the indicated dimensions.
div.style.left = sw.x + 'px';
div.style.top = ne.y + 'px';
div.style.width = (ne.x - sw.x) + 'px';
div.style.height = (sw.y - ne.y) + 'px';
},
onRemove : function() {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
}
});
MapCheckApl.prototype.MapControl = Class.create();
MapCheckApl.prototype.MapControl.prototype = {
initialize : function() {
},
};
var mapCheckApl = null;
function onLoadBody() {
mapCheckApl = new MapCheckApl;
$('buttonOverlay1').observe('click', mapCheckApl.overlayImages.bind(mapCheckApl));
$('inputLatLng1').observe('my:input', mapCheckApl.inputLatLng.bind(mapCheckApl));
$('inputMatRow1').observe('my:input', mapCheckApl.inputMatRow.bind(mapCheckApl));
$('inputMatCol1').observe('my:input', mapCheckApl.inputMatCol.bind(mapCheckApl));
$('inputInputDir1').observe('my:input', mapCheckApl.inputInputDir.bind(mapCheckApl));
// HTMLに書いたデフォルト値を反映させるため
$('inputLatLng1').fire('my:input');
$('inputMatRow1').fire('my:input');
$('inputMatCol1').fire('my:input');
$('inputInputDir1').fire('my:input');
}