Skip to content

Commit 79741b6

Browse files
author
interactive-game-maps
committed
git subrepo pull common
subrepo: subdir: "common" merged: "3e799e5" upstream: origin: "[email protected]:interactive-game-maps/common.git" branch: "master" commit: "3e799e5" git-subrepo: version: "0.4.9" origin: "???" commit: "???"
1 parent 9b65b6f commit 79741b6

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

common/.gitrepo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[subrepo]
77
remote = [email protected]:interactive-game-maps/common.git
88
branch = master
9-
commit = 6803ba27211901a896a5496679f62f3410a1e6be
10-
parent = 128fbf2ba44392a7f4d3254c034e6c7537d77ee6
9+
commit = 3e799e5362c40a0297b779f71f98e3436c14c569
10+
parent = 9b65b6fb469e6302e990c771d818ff56236bddb6
1111
method = merge
12-
cmdver = 0.4.3
12+
cmdver = 0.4.9

common/interactive_map.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ class InteractiveMap {
9191
minNativeZoom: 3,
9292
maxNativeZoom: 5,
9393
noWrap: true,
94-
detectRetina: true
94+
detectRetina: true,
95+
bounds: this.#getTileLayerBounds(url),
9596
}
97+
98+
console.log(defaults.bounds)
99+
96100
let params = { ...defaults, ...args };
97101
params.maxNativeZoom = L.Browser.retina ? params.maxNativeZoom - 1 : params.maxNativeZoom; // 1 level LOWER for high pixel ratio device.
98102

@@ -524,4 +528,50 @@ class InteractiveMap {
524528

525529
return interactive_layer;
526530
}
531+
532+
/**
533+
* Tries to read an adjacent tilemapresource.xml and calculate the bounds for this tile layer.
534+
*
535+
* Falls back to 256.
536+
*
537+
* @param {string} url Location of the tiles
538+
*/
539+
#getTileLayerBounds(url) {
540+
if (window.location.protocol !== 'file:') {
541+
// This request has to be synchronous because we can't set the tile layer bounds after initialization
542+
const request = new XMLHttpRequest();
543+
request.open("GET", url.replace("{z}/{x}/{y}.png", "tilemapresource.xml"), false); // `false` makes the request synchronous
544+
request.send(null);
545+
546+
if (request.status === 200) {
547+
try {
548+
const parser = new DOMParser();
549+
const xmlDoc = parser.parseFromString(request.responseText, "text/xml");
550+
const boundingBox = xmlDoc.getElementsByTagName("BoundingBox")[0];
551+
const reducedBounds = this.#reduceTileSizeBelow256(Math.abs(boundingBox.getAttribute("miny")), Math.abs(boundingBox.getAttribute("maxx")));
552+
553+
return L.latLngBounds(L.latLng(0, 0), L.latLng(-reducedBounds[0], reducedBounds[1]));
554+
} catch {
555+
console.log("Failed reading tilemapresource.xml");
556+
}
557+
}
558+
}
559+
560+
return L.latLngBounds(L.latLng(0, 0), L.latLng(-256, 256)); // gdal2tiles.py never produces tiles larger than 256
561+
}
562+
563+
/**
564+
* Takes a two numbers and halfs them simultaneously until both are smaller than 256.
565+
* @param {number} size1 Number to minify
566+
* @param {number} size2 Number to minify similarly
567+
* @returns [number, number]
568+
*/
569+
#reduceTileSizeBelow256(size1, size2) {
570+
while (size1 > 256 || size2 > 256) {
571+
size1 = Math.floor(size1 / 2);
572+
size2 = Math.floor(size2 / 2);
573+
}
574+
575+
return [size1, size2];
576+
}
527577
}

0 commit comments

Comments
 (0)