-
Notifications
You must be signed in to change notification settings - Fork 0
/
LayerGroup.TileMarker.js
486 lines (370 loc) · 14.1 KB
/
LayerGroup.TileMarker.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
;(function(){
/**
* A Container for dynamically loading markers.
* A hybrid of L.LayerTile behavior to manage which Markers to display and data to request.
*
* TODO - Create a data cache, so that we are not re-requesting the same data / tiles
*
*/
/**
* TODO - Remove jQuery as a dependency.
* @requires jQuery
*/
L.LayerGroup.TileMarker = L.LayerGroup.extend({
includes: L.Mixin.Events,
/**
* From TileLayer
*/
options: {
minZoom: 0,
maxZoom: 18,
tileSize: 256,
subdomains: 'abc',
errorTileUrl: '',
attribution: '',
zoomOffset: 0,
opacity: 1,
/* (undefined works too)
zIndex: null,
tms: false,
continuousWorld: false,
noWrap: false,
zoomReverse: false,
detectRetina: false,
*/
unloadInvisibleTiles: true, //L.Browser.mobile,
updateWhenIdle: L.Browser.mobile,
/**
* icon
*/
icon : null
},
initialize : function(url, options) {
L.LayerGroup.prototype.initialize.call(this); // initialize w/o layers, since we will request the points dynamically for the given bounds
this._url = url;
this._tileData = {}; // internal cache - TODO : verify this is memory safe when we have real points.
// From L.TileLayer#initialize
options = L.setOptions(this, options);
// detecting retina displays, adjusting tileSize and zoom levels
if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
options.tileSize = Math.floor(options.tileSize / 2);
options.zoomOffset++;
if (options.minZoom > 0) {
options.minZoom--;
}
this.options.maxZoom--;
}
},
onAdd : function(map){
L.LayerGroup.prototype.onAdd.apply(this, arguments);
// From L.TileLayer
// set up events
map.on({
'viewreset': this._resetCallback,
'moveend': this._update
}, this);
if (!this.options.updateWhenIdle) {
this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
map.on('move', this._limitedUpdate, this);
}
this._reset();
this._update();
},
onRemove : function(map) {
L.LayerGroup.prototype.onRemove.apply(this, arguments);
// From L.TileLayer
map.off({
'viewreset': this._resetCallback,
'moveend': this._update
}, this);
if (!this.options.updateWhenIdle) {
map.off('move', this._limitedUpdate, this);
}
this._map = null;
},
addTo : function() {
L.LayerGroup.prototype.addTo.apply(this, arguments);
// request json for marker points
},
_resetCallback : function(e){
this._reset();
},
_reset : function() {
var tiles = this._tiles;
for (var key in tiles) {
if (tiles.hasOwnProperty(key)) {
this.fire('tileunload', {tile: tiles[key]});
}
}
this._tiles = {};
this._tilesToLoad = 0;
this.clearLayers();
},
/**
* Based on L.TileLayer#_update
* @private
*/
_update : function(){
if (!this._map) { return; }
var bounds = this._map.getPixelBounds(),
zoom = this._map.getZoom(),
tileSize = this.options.tileSize;
if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
return;
}
var nwTilePoint = new L.Point(
Math.floor(bounds.min.x / tileSize),
Math.floor(bounds.min.y / tileSize)),
seTilePoint = new L.Point(
Math.floor(bounds.max.x / tileSize),
Math.floor(bounds.max.y / tileSize)),
tileBounds = new L.Bounds(nwTilePoint, seTilePoint);
this._addTilesFromCenterOut(tileBounds);
if (this.options.unloadInvisibleTiles) {
this._removeOtherTiles(tileBounds);
}
},
/**
* Just like L.TileLayer except a 'tile' DOM element is not created.
* Our Tiles are virtual: collections of markers grouped by bounds.
*
* @param bounds
* @private
*/
_addTilesFromCenterOut: function (bounds) {
var queue = [],
center = bounds.getCenter();
var j, i, point;
for (j = bounds.min.y; j <= bounds.max.y; j++) {
for (i = bounds.min.x; i <= bounds.max.x; i++) {
point = new L.Point(i, j);
if (this._tileShouldBeLoaded(point)) {
queue.push(point);
}
}
}
var tilesToLoad = queue.length;
if (tilesToLoad === 0) { return; }
// load tiles in order of their distance to center
queue.sort(function (a, b) {
return a.distanceTo(center) - b.distanceTo(center);
});
// if its the first batch of tiles to load
if (!this._tilesToLoad) {
this.fire('loading');
}
this._tilesToLoad += tilesToLoad;
for (i = 0; i < tilesToLoad; i++) {
this._addTile(queue[i]);
}
},
_removeOtherTiles: function (bounds) {
var kArr, x, y, key;
for (key in this._tiles) {
if (this._tiles.hasOwnProperty(key)) {
kArr = key.split(':');
x = parseInt(kArr[0], 10);
y = parseInt(kArr[1], 10);
// remove tile if it's out of bounds
if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
this._removeTile(key); // TODO this is broken in IE. Null Tile.
}
}
}
},
_removeTile: function (key) {
var self = this,
tile = this._tiles[key];
if (!tile){
// Already removed or never added.
// IE seems to be in some cases calling update handler duplicate times.
return;
}
this.fire("tileunload", {tile: tile, url: tile.url});
var markers = tile.markers || [];
$.each(markers, function(i, marker){
self.removeLayer(marker);
});
delete this._tiles[key];
},
_tileShouldBeLoaded: function (tilePoint) {
if ((tilePoint.x + ':' + tilePoint.y) in this._tiles) {
return false; // already loaded
}
// TODO - @see also map's cfg for jumpWorldCopy
if (!this.options.continuousWorld) {
var limit = this._getWrapTileNum();
if (this.options.noWrap && (tilePoint.x < 0 || tilePoint.x >= limit) ||
tilePoint.y < 0 || tilePoint.y >= limit) {
return false; // exceeds world bounds
}
}
return true;
},
_getWrapTileNum : function(){
return L.TileLayer.prototype._getWrapTileNum.apply(this, arguments);
},
_getZoomForUrl: function(){
return L.TileLayer.prototype._getZoomForUrl.apply(this, arguments);
},
_getTilePos: function (tilePoint) {
return L.TileLayer.prototype._getTilePos.apply(this, arguments);
},
_adjustTilePoint : function(){
return L.TileLayer.prototype._adjustTilePoint.apply(this, arguments);
},
_getSubdomain : function(){
return L.TileLayer.prototype._getSubdomain.apply(this, arguments);
},
_addTile: function (tilePoint) {
// var tilePos = this._getTilePos(tilePoint);
var tile = {
tilePoint : tilePoint
}; // markers - for easily removing when the virtual tile is removed
var key = tilePoint.x + ':' + tilePoint.y;
this._tiles[key] = tile;
tile.key = key;
this._loadTile(tile);
},
_getTileBoundsForPoint : function(tilePoint) {
var options = this.options,
tileSize = options.tileSize;
var nwPoint = tilePoint.multiplyBy(tileSize);
var sePoint = nwPoint.add(new L.Point(tileSize, tileSize));
// optionally, enlarge request area.
// with this I can draw points with coords outside this tile area,
// but with part of the graphics actually inside this tile.
// NOTE: that you should use this option only if you're actually drawing points!
var buf = this.options.buffer;
if (buf > 0) {
var diff = new L.Point(buf, buf);
nwPoint = nwPoint.subtract(diff);
sePoint = sePoint.add(diff);
}
var zoom = this._map.getZoom();
var nwCoord = this._map.unproject(nwPoint, zoom, true);
var seCoord = this._map.unproject(sePoint, zoom, true);
var bounds = [nwCoord.lng, seCoord.lat, seCoord.lng, nwCoord.lat];
return bounds;
},
/**
* Override
* @param tile
* @returns {*}
*/
getTileUrl: function (tile) {
this._adjustTilePoint(tile.tilePoint); // mutator
// This tile's bounds
var bounds = this._getTileBoundsForPoint(tile.tilePoint);
tile.bounds = bounds;
// Convert to sw, ne
var neLatLng = new L.LatLng(bounds[3], bounds[2]);
var swLatLng = new L.LatLng(bounds[1], bounds[0]);
tile.latLngBounds = new L.LatLngBounds(swLatLng, neLatLng);
var zoom = this._getZoomForUrl();
// URL Format '/markers/assets.json?sw={swLng},{swLat}&ne={neLng},{neLat}&zoom={z}'
return L.Util.template(this._url, L.extend({
neLat : neLatLng.lat,
neLng : neLatLng.lng,
swLat : swLatLng.lat,
swLng : swLatLng.lng,
z : zoom
}, this.options));
// Url Format '/markers/{tlx}/{tly}/{brx}/{bry}'
// return L.Util.template(this._url, L.extend({
// tlx: bounds[0],
// tly: bounds[1],
// brx: bounds[2],
// bry: bounds[3]
// }, this.options));
// Url Format '/markers/{z}/{x}/{y}'
// return L.Util.template(this._url, L.extend({
// s: this._getSubdomain(tilePoint),
// z: this._getZoomForUrl(),
// x: tilePoint.x,
// y: tilePoint.y
// }, this.options));
},
_loadTile: function (tile) {
var self = this;
var url = this.getTileUrl(tile);
tile.url = url;
var onSuccess = function(data) {
self._tileData[url] = data;
self._tileOnLoad(tile, data);
};
var onFailure = function(data) {
self._tileOnError(tile, data);
};
var tileData = this._tileData[url];
if (tileData) {
onSuccess(tileData);
} else {
$.when($.getJSON(url))
.then(onSuccess, onFailure);
}
},
_tileLoaded: function () {
this._tilesToLoad--;
if (!this._tilesToLoad) {
this.fire('load');
}
},
_isTileVisible : function(tile) {
var mapBounds = this._map.getBounds();
var paddedBounds = mapBounds.pad(.30);
//var isVisible = tile.latLngBounds.intersects(paddedBounds);
return tile.latLngBounds.intersects(mapBounds);
},
_tileOnLoad: function (tile, geoJSON) {
var self = this;
tile.geoJSON = geoJSON;
if (!this._isTileVisible(tile)){
var tileKey = tile.key;
self._removeTile(tileKey);
return;
}
var markers = tile.markers = this._createMarkers(geoJSON);
$.each(markers, function(i, marker){
self.addLayer(marker);
});
this.fire('tileload', {
tile: tile
});
this._tileLoaded();
},
_createMarkers : function(geoJSON) {
if (!geoJSON) {
return;
}
var getLatlng = function(feature) {
if (!feature.geometry || feature.geometry.type !== 'Point') {
return false;
}
var coords = feature.geometry.coordinates;
latlng = L.GeoJSON.coordsToLatLng(coords);
return latlng;
};
var icon = this.options.icon;
var markers = [],
features = geoJSON.features || [];
for (var i = 0, n = features.length; i<n; i++) {
var feature = features[i];
var latlng = getLatlng(feature);
var options = (!!icon) ? {
icon : icon
} : null;
var marker = new L.Marker(latlng, options);
markers.push(marker);
}
return markers;
},
_tileOnError: function (tile, data) {
this.fire('tileerror', {
tile: tile
});
// TODO should we handle with '?' display for the tile ??
this._tileLoaded();
}
});
})();