-
Notifications
You must be signed in to change notification settings - Fork 152
/
provider.js
154 lines (126 loc) · 5.31 KB
/
provider.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
//////////////////////////// Providers
MM.MapProvider = function(getTileUrl) {
if (getTileUrl) {
this.getTileUrl = getTileUrl;
}
};
MM.MapProvider.prototype = {
// defaults to Google-y Mercator style maps
projection: new MM.MercatorProjection( 0,
MM.deriveTransformation(-Math.PI, Math.PI, 0, 0,
Math.PI, Math.PI, 1, 0,
-Math.PI, -Math.PI, 0, 1) ),
tileWidth: 256,
tileHeight: 256,
// these are limits for available *tiles*
// panning limits will be different (since you can wrap around columns)
// but if you put Infinity in here it will screw up sourceCoordinate
topLeftOuterLimit: new MM.Coordinate(0,0,0),
bottomRightInnerLimit: new MM.Coordinate(1,1,0).zoomTo(18),
getTileUrl: function(coordinate) {
console && console.log("Abstract method not implemented by subclass.");
},
getTileElement: function(coordinate)
{
console && console.log("Abstract method not implemented by subclass.");
},
releaseTileElement: function(element)
{
console && console.log("Abstract method not implemented by subclass.");
},
locationCoordinate: function(location) {
return this.projection.locationCoordinate(location);
},
coordinateLocation: function(location) {
return this.projection.coordinateLocation(location);
},
outerLimits: function() {
return [ this.topLeftOuterLimit.copy(),
this.bottomRightInnerLimit.copy() ];
},
// use this to tell MapProvider that tiles only exist between certain zoom levels.
// Map will respect thse zoom limits and not allow zooming outside this range
setZoomRange: function(minZoom, maxZoom) {
this.topLeftOuterLimit = this.topLeftOuterLimit.zoomTo(minZoom);
this.bottomRightInnerLimit = this.bottomRightInnerLimit.zoomTo(maxZoom);
},
sourceCoordinate: function(coord) {
var TL = this.topLeftOuterLimit.zoomTo(coord.zoom);
var BR = this.bottomRightInnerLimit.zoomTo(coord.zoom);
var vSize = BR.row - TL.row;
if (coord.row < 0 | coord.row >= vSize) {
// it's too high or too low:
return null;
}
var hSize = BR.column - TL.column;
// assume infinite horizontal scrolling
var wrappedColumn = coord.column % hSize;
while (wrappedColumn < 0) {
wrappedColumn += hSize;
}
return new MM.Coordinate(coord.row, wrappedColumn, coord.zoom);
}
};
MM.TemplatedMapProvider = function(template, subdomains)
{
MM.MapProvider.call(this, function(coordinate) {
coordinate = this.sourceCoordinate(coordinate);
if (!coordinate) {
return null;
}
var base = template;
if (subdomains && subdomains.length && base.indexOf("{S}") >= 0) {
var subdomain = parseInt(coordinate.zoom + coordinate.row + coordinate.column, 10) % subdomains.length;
base = base.replace('{S}', subdomains[subdomain]);
}
return base.replace('{Z}', coordinate.zoom.toFixed(0)).replace('{X}', coordinate.column.toFixed(0)).replace('{Y}', coordinate.row.toFixed(0));
});
};
MM.extend(MM.TemplatedMapProvider, MM.MapProvider);
/**
* Possible new kind of provider that deals in elements.
*/
MM.TilePaintingProvider = function(template_provider, request_manager)
{
console.log(['painting provider', template_provider, request_manager]);
this.template_provider = template_provider;
this.request_manager = request_manager;
this.divs = {};
}
MM.extend(MM.TilePaintingProvider, MM.MapProvider);
MM.TilePaintingProvider.prototype.getOnTileLoaded = function(img)
{
if(!this._onTileLoaded)
{
var theProvider = this;
this._onTileLoaded = function(img)
{
if(img.id in theProvider.divs)
{
img.style.width = '100%';
img.style.height = '100%';
theProvider.divs[img.id].appendChild(img);
delete theProvider.divs[img.id];
}
}
}
return this._onTileLoaded;
}
MM.TilePaintingProvider.prototype.getTileElement = function(coord)
{
this.request_manager.requestTile(coord.toKey(), coord, this.template_provider.getTileUrl(coord), this.getOnTileLoaded());
if(coord.toKey() in this.divs)
{
return this.divs[coord.toKey()];
}
var div = document.createElement('div');
this.divs[coord.toKey()] = div;
return div;
}
MM.TilePaintingProvider.prototype.releaseTileElement = function(coord)
{
if(coord.toKey() in this.divs)
{
delete this.divs[coord.toKey()];
}
}