Skip to content

Commit f2b3194

Browse files
Allow enabling of markers via attributes (default: false) (#47)
Issue: #46
1 parent e83b7d9 commit f2b3194

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/_js/map.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
this.mapFloors = [];
77
this.markersLayers = [];
88
this.showMarkers = true;
9+
this.options = {}
910
}
1011
const URL_PREFIX = 'https://tibiamaps.github.io/tibia-map-data/';
1112
// `KNOWN_TILES` is a placeholder for the whitelist of known tiles:
@@ -200,7 +201,6 @@
200201
});
201202

202203
function getMarkersSource() {
203-
const mapContainer = document.querySelector('#map');
204204
const urlParams = new URLSearchParams(window.location.search);
205205
// Possible markers sources
206206
// A) https://example.com?markers=<base64-json-str>#32368,32198,7:0
@@ -211,8 +211,8 @@
211211
try {
212212
if (urlParams.get('markers')) return JSON.parse(atob(urlParams.get('markers')));
213213
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 mapContainer.dataset.markersUrl;
214+
if (_this.options.markers) return JSON.parse(_this.options.markers);
215+
if (_this.options.markersUrl) return _this.options.markersUrl;
216216
} catch (error) {
217217
console.error('Invalid custom markers data. Falling back to default markers');
218218
}
@@ -263,8 +263,9 @@
263263
};
264264

265265

266-
TibiaMap.prototype.init = function() {
266+
TibiaMap.prototype.init = function(options) {
267267
const _this = this;
268+
_this.options = options;
268269
modifyLeaflet();
269270
// Taken from https://tibiamaps.github.io/tibia-map-data/bounds.json, which
270271
// rarely (if ever) changes.
@@ -372,15 +373,19 @@
372373
L.ExivaButton.btns = L.exivaButton({
373374
crosshairs: this.crosshairs
374375
}).addTo(map);
375-
L.MarkersButton.btns = L.markersButton({
376-
map: _this
377-
}).addTo(map);
378376
_this._showHoverTile();
379-
_this._loadMarkers();
377+
378+
if (_this.options.markersEnabled === 'true') {
379+
L.MarkersButton.btns = L.markersButton({
380+
map: _this
381+
}).addTo(map);
382+
_this._loadMarkers();
383+
}
380384
};
381385

386+
const mapContainer = document.querySelector('#map');
382387
const map = new TibiaMap();
383-
map.init();
388+
map.init(mapContainer.dataset);
384389
L.LevelButtons.btns.setTibiaMap(map);
385390

386391
const fakeClick = function(target) {

src/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
<link rel="icon" href="favicon.ico">
2121
</head>
2222
<body>
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="https://tibiamaps.github.io/tibia-map-data/poi-markers.json"></div>-->
25-
<div id="map"></div>
23+
<!--<div id="map" data-markers-enabled="true" 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-enabled="true" data-markers-url="https://tibiamaps.github.io/tibia-map-data/poi-markers.json"></div>-->
25+
<div id="map" data-markers-enabled="true"></div>
2626
<script src="../dist/map.js"></script>
2727
</body>
2828
</html>

0 commit comments

Comments
 (0)