forked from migurski/canvas-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modestmaps.js
1149 lines (916 loc) · 37.4 KB
/
modestmaps.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// namespacing!
if (!com) {
var com = { };
if (!com.modestmaps) {
com.modestmaps = {};
}
}
//////////////////////////// Make inheritance bearable
com.modestmaps.extend = function(child, parent) {
for (var property in parent.prototype) {
if (typeof child.prototype[property] == "undefined") {
child.prototype[property] = parent.prototype[property];
}
}
return child;
};
/////////////////////////// Eeeeeeeeeeeeeeeeeeeeeevents
com.modestmaps.cancelEvent = function(e) {
//console.log('cancel: ' + e);
// there's more than one way to skin this cat
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
return false;
};
// see http://ejohn.org/apps/jselect/event.html for the originals
com.modestmaps.addEvent = function( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
}
else {
obj.addEventListener( type, fn, false );
if (type == 'mousewheel') {
obj.addEventListener( 'DOMMouseScroll', fn, false );
}
}
};
com.modestmaps.removeEvent = function( obj, type, fn ) {
if ( obj.detachEvent ) {
obj.detachEvent( 'on'+type, obj[type+fn] );
obj[type+fn] = null;
}
else {
obj.removeEventListener( type, fn, false );
if (type == 'mousewheel') {
obj.removeEventListener( 'DOMMouseScroll', fn, false );
}
}
};
//////////////////////////// Core
com.modestmaps.Point = function(x, y) {
this.x = parseFloat(x);
this.y = parseFloat(y);
};
com.modestmaps.Point.prototype = {
x: 0,
y: 0,
toString: function() {
return "(" + this.x.toFixed(3) + ", " + this.y.toFixed(3) + ")";
}
};
com.modestmaps.Coordinate = function(row, column, zoom) {
this.row = row;
this.column = column;
this.zoom = zoom;
};
com.modestmaps.Coordinate.prototype = {
row: 0,
column: 0,
zoom: 0,
toString: function() {
return "(" + this.row.toFixed(3) + ", " + this.column.toFixed(3) + " @" + this.zoom.toFixed(3) + ")";
},
toKey: function(optionalPrefix) {
var a = parseInt(this.row);
var b = parseInt(this.column);
var c = parseInt(this.zoom);
a=a-b; a=a-c; a=a^(c >>> 13);
b=b-c; b=b-a; b=b^(a << 8);
c=c-a; c=c-b; c=c^(b >>> 13);
a=a-b; a=a-c; a=a^(c >>> 12);
b=b-c; b=b-a; b=b^(a << 16);
c=c-a; c=c-b; c=c^(b >>> 5);
a=a-b; a=a-c; a=a^(c >>> 3);
b=b-c; b=b-a; b=b^(a << 10);
c=c-a; c=c-b; c=c^(b >>> 15);
if(optionalPrefix)
{
c = optionalPrefix + c;
}
return c;
},
copy: function() {
return new com.modestmaps.Coordinate(this.row, this.column, this.zoom);
},
container: function() {
return new com.modestmaps.Coordinate(Math.floor(this.row), Math.floor(this.column), Math.floor(this.zoom));
},
zoomTo: function(destination) {
var power = Math.pow(2, destination - this.zoom);
return new com.modestmaps.Coordinate(this.row * power,
this.column * power,
destination);
},
zoomBy: function(distance) {
var power = Math.pow(2, distance);
return new com.modestmaps.Coordinate(this.row * power,
this.column * power,
this.zoom + distance);
},
up: function(distance) {
if (distance == undefined) distance = 1;
return new com.modestmaps.Coordinate(this.row - distance, this.column, this.zoom);
},
right: function(distance) {
if (distance == undefined) distance = 1;
return new com.modestmaps.Coordinate(this.row, this.column + distance, this.zoom);
},
down: function(distance) {
if (distance == undefined) distance = 1;
return new com.modestmaps.Coordinate(this.row + distance, this.column, this.zoom);
},
left: function(distance) {
if (distance == undefined) distance = 1;
return new com.modestmaps.Coordinate(this.row, this.column - distance, this.zoom);
}
};
//////////////////////////// Geo
com.modestmaps.Location = function(lat, lon) {
this.lat = parseFloat(lat);
this.lon = parseFloat(lon);
};
com.modestmaps.Location.prototype = {
lat: 0,
lon: 0,
toString: function() {
return "(" + this.lat.toFixed(3) + ", " + this.lon.toFixed(3) + ")";
}
};
com.modestmaps.Transformation = function(ax, bx, cx, ay, by, cy) {
this.ax = ax;
this.bx = bx;
this.cx = cx;
this.ay = ay;
this.by = by;
this.cy = cy;
};
com.modestmaps.Transformation.prototype = {
ax: 0,
bx: 0,
cx: 0,
ay: 0,
by: 0,
cy: 0,
transform: function(point) {
return new com.modestmaps.Point(this.ax*point.x + this.bx*point.y + this.cx,
this.ay*point.x + this.by*point.y + this.cy);
},
untransform: function(point) {
return new com.modestmaps.Point((point.x*this.by - point.y*this.bx - this.cx*this.by + this.cy*this.bx) / (this.ax*this.by - this.ay*this.bx),
(point.x*this.ay - point.y*this.ax - this.cx*this.ay + this.cy*this.ax) / (this.bx*this.ay - this.by*this.ax));
},
deriveTransformation: function(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, c1x, c1y, c2x, c2y) {
// Generates a transform based on three pairs of points, a1 -> a2, b1 -> b2, c1 -> c2.
var x = this.linearSolution(a1x, a1y, a2x, b1x, b1y, b2x, c1x, c1y, c2x);
var y = this.linearSolution(a1x, a1y, a2y, b1x, b1y, b2y, c1x, c1y, c2y);
return new com.modestmaps.Transformation(x[0], x[1], x[2], y[0], y[1], y[2]);
},
linearSolution: function(r1, s1, t1, r2, s2, t2, r3, s3, t3) {
/* Solves a system of linear equations.
t1 = (a * r1) + (b + s1) + c
t2 = (a * r2) + (b + s2) + c
t3 = (a * r3) + (b + s3) + c
r1 - t3 are the known values.
a, b, c are the unknowns to be solved.
returns the a, b, c coefficients.
*/
// make them all floats
r1 = parseFloat(r1);
s1 = parseFloat(s1);
t1 = parseFloat(t1);
r2 = parseFloat(r2);
s2 = parseFloat(s2);
t2 = parseFloat(t2);
r3 = parseFloat(r3);
s3 = parseFloat(s3);
t3 = parseFloat(t3);
var a = (((t2 - t3) * (s1 - s2)) - ((t1 - t2) * (s2 - s3))) / (((r2 - r3) * (s1 - s2)) - ((r1 - r2) * (s2 - s3)));
var b = (((t2 - t3) * (r1 - r2)) - ((t1 - t2) * (r2 - r3))) / (((s2 - s3) * (r1 - r2)) - ((s1 - s2) * (r2 - r3)));
var c = t1 - (r1 * a) - (s1 * b);
return [ a, b, c ];
}
};
com.modestmaps.Projection = function(zoom, transformation) {
if (!transformation) transformation = com.modestmaps.Transformation(1, 0, 0, 0, 1, 0);
this.zoom = zoom;
this.transformation = transformation;
};
com.modestmaps.Projection.prototype = {
zoom: 0,
transformation: null,
rawProject: function(point) {
alert("Abstract method not implemented by subclass.");
},
rawUnproject: function(point) {
alert("Abstract method not implemented by subclass.");
},
project: function(point) {
point = this.rawProject(point);
if(this.transformation) {
point = this.transformation.transform(point);
}
return point;
},
unproject: function(point) {
if(this.transformation) {
point = this.transformation.untransform(point);
}
point = this.rawUnproject(point);
return point;
},
locationCoordinate: function(location) {
var point = new com.modestmaps.Point(Math.PI * location.lon / 180.0, Math.PI * location.lat / 180.0);
point = this.project(point);
return new com.modestmaps.Coordinate(point.y, point.x, this.zoom);
},
coordinateLocation: function(coordinate) {
coordinate = coordinate.zoomTo(this.zoom);
var point = new com.modestmaps.Point(coordinate.column, coordinate.row);
point = this.unproject(point);
return new com.modestmaps.Location(180.0 * point.y / Math.PI, 180.0 * point.x / Math.PI);
}
};
com.modestmaps.LinearProjection = function(zoom, transformation) {
com.modestmaps.Projection.call(this, zoom, transformation);
};
com.modestmaps.LinearProjection.prototype = {
rawProject: function(point) {
return new com.modestmaps.Point(point.x, point.y);
},
rawUnproject: function(point) {
return new com.modestmaps.Point(point.x, point.y);
}
};
com.modestmaps.extend(com.modestmaps.LinearProjection, com.modestmaps.Projection);
com.modestmaps.MercatorProjection = function(zoom, transformation) {
// super!
com.modestmaps.Projection.call(this, zoom, transformation);
};
com.modestmaps.MercatorProjection.prototype = {
rawProject: function(point) {
return new com.modestmaps.Point(point.x,
Math.log(Math.tan(0.25 * Math.PI + 0.5 * point.y)));
},
rawUnproject: function(point) {
return new com.modestmaps.Point(point.x,
2 * Math.atan(Math.pow(Math.E, point.y)) - 0.5 * Math.PI);
}
};
com.modestmaps.extend(com.modestmaps.MercatorProjection, com.modestmaps.Projection);
//////////////////////////// Providers
com.modestmaps.MapProvider = function(getTileUrl) {
if (getTileUrl) {
this.getTileUrl = getTileUrl;
}
};
com.modestmaps.MapProvider.prototype = {
// defaults to Google-y Mercator style maps
// see http://modestmaps.com/calculator.html for how to generate these magic numbers
projection: new com.modestmaps.MercatorProjection(26, new com.modestmaps.Transformation(1.068070779e7, 0, 3.355443185e7, 0, -1.068070890e7, 3.355443057e7)),
tileWidth: 256,
tileHeight: 256,
getTileUrl: function(coordinate) {
alert("Abstract method not implemented by subclass.");
},
locationCoordinate: function(location) {
return this.projection.locationCoordinate(location);
},
coordinateLocation: function(location) {
return this.projection.coordinateLocation(location);
},
sourceCoordinate: function(coordinate) {
var wrappedColumn = coordinate.column % Math.pow(2, coordinate.zoom);
while (wrappedColumn < 0) {
wrappedColumn += Math.pow(2, coordinate.zoom);
}
return new com.modestmaps.Coordinate(coordinate.row, wrappedColumn, coordinate.zoom);
}
};
com.modestmaps.BlueMarbleProvider = function() {
com.modestmaps.MapProvider.call(this, function(coordinate) {
var img = coordinate.zoom.toFixed(0) +'-r'+ coordinate.row.toFixed(0) +'-c'+ coordinate.column.toFixed(0) + '.jpg';
return 'http://s3.amazonaws.com/com.modestmaps.bluemarble/' + img;
});
};
com.modestmaps.extend(com.modestmaps.BlueMarbleProvider, com.modestmaps.MapProvider);
//////////////////////////// Map
com.modestmaps.Map = function(parent, provider, dimensions) {
/* Instance of a map intended for drawing to a div.
parent
DOM element
provider
Instance of IMapProvider
dimensions
Size of output image, instance of Point
*/
if (typeof parent == 'string') {
parent = document.getElementById(parent);
}
this.parent = parent;
this.idBase = parent.id;
this.parent.style.position = 'relative';
this.parent.style.width = parseInt(dimensions.x) + 'px';
this.parent.style.height = parseInt(dimensions.y) + 'px';
this.parent.style.padding = '0';
this.parent.style.overflow = 'hidden';
this.parent.style.backgroundColor = '#eee';
com.modestmaps.addEvent(this.parent, 'dblclick', this.getDoubleClick());
com.modestmaps.addEvent(this.parent, 'mousedown', this.getMouseDown());
com.modestmaps.addEvent(this.parent, 'mousewheel', this.getMouseWheel());
// add an invisible layer so that image.onload will have a srcElement in IE6
this.loadingLayer = document.createElement('div');
this.loadingLayer.id = this.idBase+'loading layer';
this.loadingLayer.style.display = 'none';
this.parent.appendChild(this.loadingLayer);
this.layers = [];
// add a div for each zoom level
for (var z = 0; z <= 20; z++) {
var layer = document.createElement('div');
layer.id = this.idBase+'zoom-'+z;
layer.style.margin = '0';
layer.style.padding = '0';
layer.style.width = '100%';
layer.style.height = '100%';
layer.style.position = 'absolute';
layer.style.top = '0px';
layer.style.left = '0px';
this.parent.appendChild(layer);
this.layers.push(layer);
}
this.provider = provider;
this.dimensions = dimensions;
this.coordinate = new com.modestmaps.Coordinate(0.5,0.5,0);
this.tiles = {};
this.requestedTiles = {};
this.requestCount = 0;
this.maxSimultaneousRequests = 4;
this.requestQueue = [];
this.tileCacheSize = 0;
this.callbacks = { zoomed: [], panned: [], centered: [], extentset: [] };
};
com.modestmaps.Map.prototype = {
parent: null,
provider: null,
dimensions: null,
coordinate: null,
tiles: null,
requestedTiles: null,
layers: null,
requestCount: null,
maxSimultaneousRequests: null,
requestQueue: null,
tileCacheSize: null,
callbacks: null,
toString: function() {
return 'Map(' + this.provider.toString() + this.dimensions.toString() + this.coordinate.toString() + ')';
},
addCallback: function(event, callback)
{
if (typeof(callback) == 'function' && this.callbacks[event]) {
this.callbacks[event].push(callback);
}
},
dispatchCallback: function(event, message)
{
if(this.callbacks[event]) {
for (var i = 0; i < this.callbacks[event].length; i += 1) {
try {
this.callbacks[event][i](this, message);
} catch(e) {
// meh
}
}
}
},
createOverlay: function(id)
{
var canvas = document.createElement('canvas');
canvas.id = id;
canvas.width = this.dimensions.x;
canvas.height = this.dimensions.y;
canvas.style.margin = '0';
canvas.style.padding = '0';
canvas.style.position = 'absolute';
canvas.style.top = '0px';
canvas.style.left = '0px';
this.parent.appendChild(canvas);
},
// events
mouseDownHandler: null,
getMouseDown: function() {
if (!this.mouseDownHandler) {
var theMap = this;
this.mouseDownHandler = function(e) {
if (!e) var e = window.event;
com.modestmaps.addEvent(document, 'mouseup', theMap.getMouseUp());
com.modestmaps.addEvent(document, 'mousemove', theMap.getMouseMove());
theMap.prevMouse = new com.modestmaps.Point(e.clientX, e.clientY);
theMap.parent.style.cursor = 'move';
return com.modestmaps.cancelEvent(e);
};
}
return this.mouseDownHandler;
},
mouseMoveHandler: null,
getMouseMove: function() {
if (!this.mouseMoveHandler) {
var theMap = this;
this.mouseMoveHandler = function(e) {
if (!e) e = window.event;
if (theMap.prevMouse) {
theMap.panBy(e.clientX - theMap.prevMouse.x, e.clientY - theMap.prevMouse.y);
theMap.prevMouse.x = e.clientX;
theMap.prevMouse.y = e.clientY;
}
return com.modestmaps.cancelEvent(e);
};
}
return this.mouseMoveHandler;
},
mouseUpHandler: null,
getMouseUp: function() {
if (!this.mouseUpHandler) {
var theMap = this;
this.mouseUpHandler = function(e) {
if (!e) e = window.event;
com.modestmaps.removeEvent(document, 'mouseup', theMap.getMouseUp());
com.modestmaps.removeEvent(document, 'mousemove', theMap.getMouseMove());
theMap.prevMouse = null;
theMap.parent.style.cursor = '';
return com.modestmaps.cancelEvent(e);
};
}
return this.mouseUpHandler;
},
mouseWheelHandler: null,
getMouseWheel: function() {
if (!this.mouseWheelHandler) {
var theMap = this;
var prevTime = new Date().getTime();
this.mouseWheelHandler = function(e) {
if (!e) e = window.event;
var delta = 0;
if (e.wheelDelta) {
delta = e.wheelDelta;
}
else if (e.detail) {
delta = -e.detail;
}
// limit mousewheeling to once every 200ms
var timeSince = new Date().getTime() - prevTime;
if (delta != 0 && (timeSince > 200)) {
var point = theMap.getMousePoint(e);
theMap.zoomByAbout(delta > 0 ? 1 : -1, point);
prevTime = new Date().getTime();
}
return com.modestmaps.cancelEvent(e);
};
}
return this.mouseWheelHandler;
},
doubleClickHandler: null,
getDoubleClick: function() {
if (!this.doubleClickHandler) {
var theMap = this;
this.doubleClickHandler = function(e) {
if (!e) e = window.event;
var point = theMap.getMousePoint(e);
// use shift-double-click to zoom out
theMap.zoomByAbout(e.shiftKey ? -1 : 1, point);
return com.modestmaps.cancelEvent(e);
};
}
return this.doubleClickHandler;
},
// interaction helper
getMousePoint: function(e)
{
// start with just the mouse (x, y)
var point = new com.modestmaps.Point(e.clientX, e.clientY);
// correct for scrolled document
point.x += document.body.scrollLeft + document.documentElement.scrollLeft;
point.y += document.body.scrollTop + document.documentElement.scrollTop;
// correct for nested offsets in DOM
for(var node = this.parent; node; node = node.offsetParent) {
point.x -= node.offsetLeft;
point.y -= node.offsetTop;
}
return point;
},
// zooming
zoomIn: function() {
this.zoomBy(1);
},
zoomOut: function() {
this.zoomBy(-1);
},
setZoom: function(z) {
this.zoomBy(z - this.coordinate.zoom);
},
zoomBy: function(zoomOffset) {
this.coordinate = this.coordinate.zoomBy(zoomOffset);
this.draw();
this.dispatchCallback('zoomed', zoomOffset);
},
zoomByAbout: function(zoomOffset, point) {
var location = this.pointLocation(point);
this.coordinate = this.coordinate.zoomBy(zoomOffset);
var newPoint = this.locationPoint(location);
this.panBy(point.x - newPoint.x, point.y - newPoint.y);
this.dispatchCallback('zoomed', zoomOffset);
},
// panning
panBy: function(dx, dy) {
this.coordinate.column -= dx / this.provider.tileWidth;
this.coordinate.row -= dy / this.provider.tileHeight;
this.draw();
this.dispatchCallback('panned', [dx, dy]);
},
panLeft: function() {
this.panBy(100,0);
},
panRight: function() {
this.panBy(-100,0);
},
panDown: function() {
this.panBy(0,-100);
},
panUp: function() {
this.panBy(0,100);
},
// positioning
setCenter: function(location) {
this.setCenterZoom(location, this.coordinate.zoom);
},
setCenterZoom: function(location, zoom) {
this.coordinate = this.provider.locationCoordinate(location).zoomTo(zoom);
this.draw();
this.dispatchCallback('centered', [location, zoom]);
},
setExtent: function(locations) {
var TL, BR;
for (var i = 0; i < locations.length; i++) {
var coordinate = this.provider.locationCoordinate(locations[i]);
if (TL) {
TL.row = Math.min(TL.row, coordinate.row);
TL.column = Math.min(TL.column, coordinate.column);
TL.zoom = Math.min(TL.zoom, coordinate.zoom);
BR.row = Math.max(BR.row, coordinate.row);
BR.column = Math.max(BR.column, coordinate.column);
BR.zoom = Math.max(BR.zoom, coordinate.zoom);
}
else {
TL = coordinate.copy();
BR = coordinate.copy();
}
}
var width = this.dimensions.x + 1;
var height = this.dimensions.y + 1;
// multiplication factor between horizontal span and map width
var hFactor = (BR.column - TL.column) / (width / this.provider.tileWidth);
// multiplication factor expressed as base-2 logarithm, for zoom difference
var hZoomDiff = Math.log(hFactor) / Math.log(2);
// possible horizontal zoom to fit geographical extent in map width
var hPossibleZoom = TL.zoom - Math.ceil(hZoomDiff);
// multiplication factor between vertical span and map height
var vFactor = (BR.row - TL.row) / (height / this.provider.tileHeight);
// multiplication factor expressed as base-2 logarithm, for zoom difference
var vZoomDiff = Math.log(vFactor) / Math.log(2);
// possible vertical zoom to fit geographical extent in map height
var vPossibleZoom = TL.zoom - Math.ceil(vZoomDiff);
// initial zoom to fit extent vertically and horizontally
var initZoom = Math.min(hPossibleZoom, vPossibleZoom);
// additionally, make sure it's not outside the boundaries set by provider limits
// initZoom = min(initZoom, provider.outerLimits()[1].zoom)
// initZoom = max(initZoom, provider.outerLimits()[0].zoom)
// coordinate of extent center
var centerRow = (TL.row + BR.row) / 2;
var centerColumn = (TL.column + BR.column) / 2;
var centerZoom = TL.zoom;
this.coordinate = new com.modestmaps.Coordinate(centerRow, centerColumn, centerZoom).zoomTo(initZoom);
this.draw();
this.dispatchCallback('extentset', locations);
},
// projecting points on and off screen
coordinatePoint: function(coord)
{
/* Return an x, y point on the map image for a given coordinate. */
if(coord.zoom != this.coordinate.zoom) {
coord = coord.zoomTo(this.coordinate.zoom);
}
// distance from the center of the map
var point = new com.modestmaps.Point(this.dimensions.x/2, this.dimensions.y/2);
point.x += this.provider.tileWidth * (coord.column - this.coordinate.column);
point.y += this.provider.tileHeight * (coord.row - this.coordinate.row);
return point;
},
pointCoordinate: function(point)
{
/* Return a coordinate on the map image for a given x, y point. */
// new point coordinate reflecting distance from map center, in tile widths
var coord = this.coordinate.copy();
coord.column += (point.x - this.dimensions.x/2) / this.provider.tileWidth;
coord.row += (point.y - this.dimensions.y/2) / this.provider.tileHeight;
return coord;
},
locationPoint: function(location)
{
/* Return an x, y point on the map image for a given geographical location. */
return this.coordinatePoint(this.provider.locationCoordinate(location));
},
pointLocation: function(point)
{
/* Return a geographical location on the map image for a given x, y point. */
return this.provider.coordinateLocation(this.pointCoordinate(point));
},
// inspecting
getExtent: function() {
var extent = [];
extent.push(this.pointLocation(new com.modestmaps.Point(0,0)));
extent.push(this.pointLocation(this.dimensions));
return extent;
},
getCenter: function() {
return this.provider.coordinateLocation(this.coordinate);
},
getZoom: function() {
return this.coordinate.zoom;
},
setProvider: function(newProvider)
{
for (var tileKey in this.requestedTiles) {
if (!wantedTiles[tileKey]) {
var tile = this.requestedTiles[tileKey];
this.cancelTileRequest(tile);
tile = null;
}
}
for(var i = 0; i < this.layers.length; i += 1) {
var layer = this.layers[i];
while(layer.firstChild) {
layer.removeChild(layer.firstChild);
}
}
this.tiles = {};
this.requestedTiles = {};
this.requestCount = 0;
this.maxSimultaneousRequests = 4;
this.requestQueue = [];
this.tileCacheSize = 0;
// empty cache ?
// for later: check geometry and set a new center (not now)
this.provider = newProvider;
this.draw();
},
// rendering
draw: function(onlyThisLayer) {
// console.log('requestQueue: ' + this.requestQueue.length);
// console.log('requestCount: ' + this.requestCount);
// console.log('tileCacheSize: ' + this.tileCacheSize);
//console.log('--- begin draw ' + onlyThisLayer);
// so this is the corner, taking the container offset into account
var baseCoord = this.coordinate.container();
var baseCorner = new com.modestmaps.Point(this.dimensions.x/2, this.dimensions.y/2);
baseCorner.x += (baseCoord.column - this.coordinate.column) * this.provider.tileWidth;
baseCorner.y += (baseCoord.row - this.coordinate.row) * this.provider.tileHeight;
// get back to the top left
while (baseCorner.x > 0) {
baseCorner.x -= this.provider.tileWidth;
baseCoord.column -= 1;
}
while (baseCorner.y > 0) {
baseCorner.y -= this.provider.tileHeight;
baseCoord.row -= 1;
}
var wantedTiles = { };
var thisLayer = document.getElementById(this.idBase+'zoom-'+parseInt(baseCoord.zoom));
thisLayer.coordinate = this.coordinate.copy();
var showParentLayer = false;
var tileCoord = baseCoord.copy();
for (var y = baseCorner.y; y < this.dimensions.y; y += this.provider.tileHeight) {
for (var x = baseCorner.x; x < this.dimensions.x; x += this.provider.tileWidth) {
var tileKey = tileCoord.toKey(this.idBase);
wantedTiles[tileKey] = true;
if (!this.tiles[tileKey]) {
if (!this.requestedTiles[tileKey]) {
this.requestTile(tileCoord);
}
showParentLayer = true;
if (!onlyThisLayer) {
for (var pz = 1; pz <= 5; pz++) {
var parentKey = tileCoord.zoomBy(-pz).container().toKey(this.idBase);
wantedTiles[parentKey] = true;
}
var childCoord = tileCoord.zoomBy(1);
wantedTiles[childCoord.toKey(this.idBase)] = true;
childCoord.column += 1;
wantedTiles[childCoord.toKey(this.idBase)] = true;
childCoord.row += 1;
wantedTiles[childCoord.toKey(this.idBase)] = true;
childCoord.column -= 1;
wantedTiles[childCoord.toKey(this.idBase)] = true;
}
}
else {
var tile = this.tiles[tileKey];
if (!document.getElementById(tile.id)) {
thisLayer.appendChild(tile);
}
tile.style.left = x + 'px';
tile.style.top = y + 'px';
}
tileCoord.column += 1;
}
tileCoord.row += 1;
tileCoord.column = baseCoord.column;
}
//console.log(showParentLayer);
if (!onlyThisLayer || !showParentLayer) {
// layers that would be scaled too big:
for (var i = 0; i < baseCoord.zoom-5; i++) {
var layer = this.layers[i];
layer.style.display = 'none';
var visibleTiles = layer.getElementsByTagName('img');
for (var j = visibleTiles.length-1; j >= 0; j--) {
layer.removeChild(visibleTiles[j]);
}
}
// layers that would be scaled too small, and tiles would be too numerous:
for (var i = baseCoord.zoom+2; i < this.layers.length; i++) {
var layer = this.layers[i];
layer.style.display = 'none';
var visibleTiles = layer.getElementsByTagName('img');
for (var j = visibleTiles.length-1; j >= 0; j--) {
layer.removeChild(visibleTiles[j]);
}
}
// layers we want to see, if they have tiles that are in wantedTiles
for (var i = Math.max(0, baseCoord.zoom-5); i < Math.min(baseCoord.zoom+2, this.layers.length); i++) {
var layer = this.layers[i];
var scale = 1;
var theCoord = null;
if (layer.coordinate) {
layer.style.display = 'block';
if (layer != thisLayer) {
theCoord = this.coordinate.zoomTo(layer.coordinate.zoom);
scale = Math.pow(2, this.coordinate.zoom - layer.coordinate.zoom);
}
}
else {
layer.style.display = 'none';
}
var visibleTiles = layer.getElementsByTagName('img');
for (var j = visibleTiles.length-1; j >= 0; j--) {
var tile = visibleTiles[j];
if (!wantedTiles[tile.id]) {
layer.removeChild(tile);
}
else if (theCoord) {
var tx = ((this.dimensions.x/2) + (tile.coord.column - theCoord.column) * this.provider.tileWidth * scale);
var ty = ((this.dimensions.y/2) + (tile.coord.row - theCoord.row) * this.provider.tileHeight * scale);
tile.style.left = parseInt(tx) + 'px';
tile.style.top = parseInt(ty) + 'px';
//tile.width = this.provider.tileWidth * scale;
//tile.height = this.provider.tileHeight * scale;
}
else {
//tile.width = this.provider.tileWidth;
//tile.height = this.provider.tileHeight;
}
}
}
}