-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-leaflet-fallback.js
More file actions
177 lines (132 loc) · 4.85 KB
/
react-leaflet-fallback.js
File metadata and controls
177 lines (132 loc) · 4.85 KB
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
import L from 'leaflet';
import React from 'react';
import {
useMap,
} from "react-leaflet";
L.TileLayer.Fallback = L.TileLayer.extend({
options: {
minNativeZoom: 0
},
initialize: function (urlTemplate, options) {
L.TileLayer.prototype.initialize.call(this, urlTemplate, options);
},
createTile: function (coords, done) {
var tile = L.TileLayer.prototype.createTile.call(this, coords, done);
tile._originalCoords = coords;
tile._originalSrc = tile.src;
tile._fallbackZoom = coords.z
return tile;
},
_createCurrentCoords: function (originalCoords) {
var currentCoords = this._wrapCoords(originalCoords);
currentCoords.fallback = true;
return currentCoords;
},
_originalTileOnError: L.TileLayer.prototype._tileOnError,
_tileOnError: function (done, tile, e) {
var layer = this,
originalCoords = tile._originalCoords,
currentCoords = (tile._currentCoords =
tile._currentCoords || layer._createCurrentCoords(originalCoords)),
fallbackZoom =
tile._fallbackZoom === undefined
? originalCoords.z - 1
: tile._fallbackZoom - 1,
scale = (tile._fallbackScale = (tile._fallbackScale || 1) * 2),
tileSize = layer.getTileSize(),
style = tile.style,
newUrl,
top,
left;
if (fallbackZoom < layer.options.minNativeZoom) {
// Handle the specific case of z/0/0 differently
if (originalCoords.x === 0 && originalCoords.y === 0) {
// Fallback behavior for coordinates (0, 0)
currentCoords.x = 1;
currentCoords.y = 1;
currentCoords.z = originalCoords.z - 1;
newUrl = layer.getTileUrl(currentCoords);
} else {
return this._originalTileOnError(done, tile, e);
}
}
currentCoords.z = fallbackZoom;
currentCoords.x = Math.floor(currentCoords.x / 2);
currentCoords.y = Math.floor(currentCoords.y / 2);
// Decrement fallbackZoom for the next iteration -- defines and updates the fallbackzoom.
tile._fallbackZoom = fallbackZoom;
newUrl = layer.getTileUrl(currentCoords);
style.width = tileSize.x * scale + 'px';
style.height = tileSize.y * scale + 'px';
top = (originalCoords.y - currentCoords.y * scale) * tileSize.y;
style.marginTop = -top + 'px';
left = (originalCoords.x - currentCoords.x * scale) * tileSize.x;
style.marginLeft = -left + 'px';
style.clip =
'rect(' +
top +
'px ' +
(left + tileSize.x) +
'px ' +
(top + tileSize.y) +
'px ' +
left +
'px)';
layer.fire('tilefallback', {
tile: tile,
url: tile._originalSrc,
urlMissing: tile.src,
urlFallback: newUrl
});
tile.src = newUrl;
},
getTileUrl: function (coords) {
var z = coords.z = coords.fallback ? coords.z : this._getZoomForUrl();
var data = {
r: L.Browser.retina ? '@2x' : '',
s: this._getSubdomain(coords),
x: coords.x,
y: coords.y,
z: z
};
if (this._map && !this._map.options.crs.infinite) {
var invertedY = this._globalTileRange.max.y - coords.y;
if (this.options.tms) {
data['y'] = invertedY;
}
data['-y'] = invertedY;
}
return L.Util.template(this._url, L.extend(data, this.options));
}
});
// Create a factory function for the fallback tile layer
L.tileLayer.fallback = function (urlTemplate, options) {
return new L.TileLayer.Fallback(urlTemplate, options);
};
const FallbackTileLayer = React.memo((props) => {
const { url, ...options } = props;
const map = useMap();
const fallbackTileLayerRef = React.useRef(null);
React.useMemo(() => {
if (!fallbackTileLayerRef.current) {
// Create the fallback tile layer using the factory function
const fallbackTileLayer = L.tileLayer.fallback(url, options);
fallbackTileLayerRef.current = fallbackTileLayer;
// Add the fallback tile layer to the Leaflet map
if (map && fallbackTileLayer) {
fallbackTileLayer.addTo(map);
} // Replace `map` with your Leaflet map instance
}
}, [url, options, map ]);
React.useEffect(() => {
// Clean up: remove the fallback tile layer from the Leaflet map when the component is unmounted
return () => {
if (fallbackTileLayerRef.current) {
fallbackTileLayerRef.current.removeFrom(map);
fallbackTileLayerRef.current = null;
}
};
}, [map]);
return null; // This component doesn't render any JSX, so return null
});
export default FallbackTileLayer;