|
199 | 199 | }); |
200 | 200 | }); |
201 | 201 |
|
202 | | - const mapContainer = document.querySelector('#map'); |
203 | | - const MARKERS_URL_PATH = mapContainer.dataset.markerPack ? mapContainer.dataset.markerPack : 'markers.json'; |
204 | | - const xhr = new XMLHttpRequest(); |
205 | | - xhr.open('GET', URL_PREFIX + MARKERS_URL_PATH); |
206 | | - xhr.responseType = 'json'; |
207 | | - xhr.onload = function() { |
208 | | - if (xhr.status === 200) { |
209 | | - xhr.response.forEach(m => { |
210 | | - const options = {'title': m.description}; |
211 | | - if (m.icon && m.icon in icons) { options.icon = icons[m.icon]; } |
212 | | - if (!_this.markersLayers[m.z]) { _this.markersLayers[m.z] = new L.layerGroup(); } |
213 | | - _this.markersLayers[m.z].addLayer( |
214 | | - L.marker(_this.map.unproject([m.x + 0.5, m.y + 0.5], 0), options) |
215 | | - ); |
216 | | - }); |
217 | | - _this._tryShowMarkers(); |
| 202 | + function getMarkersSource() { |
| 203 | + const mapContainer = document.querySelector('#map'); |
| 204 | + const urlParams = new URLSearchParams(window.location.search); |
| 205 | + // Possible markers sources |
| 206 | + // A) https://example.com?markers=<base64-json-str>#32368,32198,7:0 |
| 207 | + // B) https://example.com?markersUrl=https://example.com/pack.json#32368,32198,7:0 |
| 208 | + // C) <div id="map" data-marker="<json-str>" ...> |
| 209 | + // D) <div id="map" data-marker-url="https://example.com/pack.json" ...> |
| 210 | + // E) fallback: https://tibiamaps.github.io/tibia-map-data/markers.json |
| 211 | + try { |
| 212 | + if (urlParams.get('markers')) return JSON.parse(atob(urlParams.get('markers'))); |
| 213 | + if (urlParams.get('markersUrl')) return urlParams.get('markersUrl'); |
| 214 | + if (mapContainer.dataset.markers) return JSON.parse(mapContainer.dataset.markers); |
| 215 | + if (mapContainer.dataset.markersUrl) return URL_PREFIX + mapContainer.dataset.markersUrl; |
| 216 | + } catch (error) { |
| 217 | + console.error('Invalid custom markers data. Falling back to default markers'); |
218 | 218 | } |
219 | | - }; |
220 | | - xhr.send(); |
| 219 | + return URL_PREFIX + 'markers.json'; |
| 220 | + } |
| 221 | + |
| 222 | + const markersSource = getMarkersSource(); |
| 223 | + if (typeof markersSource === 'string') { |
| 224 | + loadMarkersFromUrl(markersSource); |
| 225 | + } else { |
| 226 | + buildMarkerLayers(markersSource); |
| 227 | + } |
| 228 | + |
| 229 | + function loadMarkersFromUrl(url) { |
| 230 | + const xhr = new XMLHttpRequest(); |
| 231 | + xhr.open('GET', url); |
| 232 | + xhr.responseType = 'json'; |
| 233 | + xhr.onload = function () { |
| 234 | + if (xhr.status === 200) { |
| 235 | + buildMarkerLayers(xhr.response); |
| 236 | + } |
| 237 | + }; |
| 238 | + xhr.send(); |
| 239 | + } |
| 240 | + |
| 241 | + function buildMarkerLayers(markersData) { |
| 242 | + markersData.forEach(m => { |
| 243 | + const options = {'title': m.description}; |
| 244 | + if (m.icon && m.icon in icons) { options.icon = icons[m.icon]; } |
| 245 | + if (!_this.markersLayers[m.z]) { _this.markersLayers[m.z] = new L.layerGroup(); } |
| 246 | + _this.markersLayers[m.z].addLayer( |
| 247 | + L.marker(_this.map.unproject([m.x + 0.5, m.y + 0.5], 0), options) |
| 248 | + ); |
| 249 | + }); |
| 250 | + _this._tryShowMarkers(); |
| 251 | + } |
221 | 252 | }; |
222 | 253 | TibiaMap.prototype._toggleMarkers = function () { |
223 | 254 | this.showMarkers = !this.showMarkers; |
|
0 commit comments