diff --git a/src/_js/map.js b/src/_js/map.js index cb7f7a8..4c20ae7 100644 --- a/src/_js/map.js +++ b/src/_js/map.js @@ -199,41 +199,44 @@ }); }); - const mapContainer = document.querySelector('#map'); - const urlParams = new URLSearchParams(window.location.search); - // Possible markers sources - // A) https://example.com?m=#32368,32198,7:0 - // B) https://example.com?mf=https://example.com/pack.json#32368,32198,7:0 - // C)
- // D)
- // E) fallback: https://tibiamaps.github.io/tibia-map-data/markers.json - if (urlParams.get('m')) { - buildMarkerLayers(atob(JSON.parse(urlParams.get('m')))); - } else if (urlParams.get('mf')) { - loadMarkersPack(urlParams.get('mf')); - } else if (mapContainer.dataset.markerJson) { - buildMarkerLayers(JSON.parse(mapContainer.dataset.markerJson)); - } else if (mapContainer.dataset.markerPack) { - loadMarkersPack(URL_PREFIX + mapContainer.dataset.markerPack); - } else { - loadMarkersPack(URL_PREFIX + 'markers.json'); + function getMarkersSource() { + const mapContainer = document.querySelector('#map'); + const urlParams = new URLSearchParams(window.location.search); + // Possible markers sources + // A) https://example.com?markers=#32368,32198,7:0 + // B) https://example.com?markersUrl=https://example.com/pack.json#32368,32198,7:0 + // C)
+ // D)
+ // E) fallback: https://tibiamaps.github.io/tibia-map-data/markers.json + try { + if (urlParams.get('markers')) return JSON.parse(atob(urlParams.get('markers'))); + if (urlParams.get('markersUrl')) return urlParams.get('markersUrl'); + if (mapContainer.dataset.markers) return JSON.parse(mapContainer.dataset.markers); + if (mapContainer.dataset.markersUrl) return URL_PREFIX + mapContainer.dataset.markersUrl; + } catch (e) { + console.error("Invalid custom markers data, using default markers") + } + return URL_PREFIX + 'markers.json'; } - function loadMarkersPack(url) { + const markersSource = getMarkersSource(); + if (typeof markersSource === 'string') { loadMarkersFromUrl(markersSource); } + else { buildMarkerLayers(markersSource); } + + function loadMarkersFromUrl(url) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.responseType = 'json'; xhr.onload = function () { if (xhr.status === 200) { buildMarkerLayers(xhr.response); - _this._tryShowMarkers(); } }; xhr.send(); } - function buildMarkerLayers(array) { - array.forEach(m => { + function buildMarkerLayers(markersData) { + markersData.forEach(m => { const options = {'title': m.description}; if (m.icon && m.icon in icons) { options.icon = icons[m.icon]; } if (!_this.markersLayers[m.z]) { _this.markersLayers[m.z] = new L.layerGroup(); } @@ -241,6 +244,7 @@ L.marker(_this.map.unproject([m.x + 0.5, m.y + 0.5], 0), options) ); }); + _this._tryShowMarkers(); } }; TibiaMap.prototype._toggleMarkers = function () { diff --git a/src/index.html b/src/index.html index 1996e79..bfbbc1d 100644 --- a/src/index.html +++ b/src/index.html @@ -20,7 +20,8 @@ - + +