Skip to content

Commit bf3bf10

Browse files
committed
support tiles in GeoJSON
1 parent 9275ca6 commit bf3bf10

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

examples/clients/leaflet/geojson-tile-layer.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
createTile: function (coords) {
2626
var tile = L.DomUtil.create('div', 'leaflet-tile');
2727
tile.style['box-shadow'] = 'inset 0 0 2px #f00';
28-
var url = L.Util.template(this.url, coords);
28+
var url = this._expandUrl(this.url, coords);
2929
if (this.cache[url]) {
3030
this._updateLayers(url, this.cache[url]);
3131
} else {
@@ -51,6 +51,36 @@
5151
//
5252
// Custom methods
5353
//
54+
_expandUrl: function(template, coords) {
55+
// from: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Implementations
56+
var tile2lon = function(x,z) {
57+
return (x/Math.pow(2,z)*360-180);
58+
};
59+
var tile2lat = function(y,z) {
60+
var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
61+
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
62+
};
63+
// from: https://leafletjs.com/reference-1.5.0.html#map-methods-for-getting-map-state
64+
var southWest = L.latLng(
65+
tile2lat(coords.y+1, coords.z),
66+
tile2lon(coords.x+1, coords.z)
67+
);
68+
var northEast = L.latLng(
69+
tile2lat(coords.y, coords.z),
70+
tile2lon(coords.x, coords.z)
71+
);
72+
// from: "toBBoxString()" on https://leafletjs.com/reference-1.5.0.html#latlngbounds
73+
var bboxStr = [southWest.lng,southWest.lat,northEast.lng,northEast.lat].join(',');
74+
coords = Object.assign(coords, {
75+
lat1: southWest.lat,
76+
lon1: southWest.lng,
77+
lat2: northEast.lat,
78+
lon2: northEast.lng,
79+
bbox: bboxStr
80+
});
81+
return L.Util.template(template, coords);
82+
},
83+
5484
_updateLayers: function(url, geoData) {
5585
if (geoData.type == 'FeatureCollection'){
5686
geoData = geoData.features;

0 commit comments

Comments
 (0)