Skip to content

Commit 0c12dfe

Browse files
Add more sources for markers (#45)
1 parent 486c5fe commit 0c12dfe

2 files changed

Lines changed: 51 additions & 19 deletions

File tree

src/_js/map.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,56 @@
199199
});
200200
});
201201

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');
218218
}
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+
}
221252
};
222253
TibiaMap.prototype._toggleMarkers = function () {
223254
this.showMarkers = !this.showMarkers;

src/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<link rel="icon" href="favicon.ico">
2121
</head>
2222
<body>
23-
<!--<div id="map" data-marker-pack="poi-markers.json"></div>-->
23+
<!--<div id="map" data-markers='[{"description":"Shops","icon":"bag","x":32368,"y":32198,"z":7},{"description":"Temple","icon":"cross","x":32369,"y":32241,"z":7}]'></div>-->
24+
<!--<div id="map" data-markers-url="poi-markers.json"></div>-->
2425
<div id="map"></div>
2526
<script src="../dist/map.js"></script>
2627
</body>

0 commit comments

Comments
 (0)