Merge pull request #13942 from MoonE/kmz

Improve loading of kmz file in example
This commit is contained in:
MoonE
2022-08-07 23:51:52 +02:00
committed by GitHub
+6 -9
View File
@@ -16,7 +16,7 @@ const zip = new JSZip();
function getKMLData(buffer) { function getKMLData(buffer) {
let kmlData; let kmlData;
zip.load(buffer); zip.load(buffer);
const kmlFile = zip.file(/.kml$/i)[0]; const kmlFile = zip.file(/\.kml$/i)[0];
if (kmlFile) { if (kmlFile) {
kmlData = kmlFile.asText(); kmlData = kmlFile.asText();
} }
@@ -24,17 +24,14 @@ function getKMLData(buffer) {
} }
function getKMLImage(href) { function getKMLImage(href) {
let url = href; const index = window.location.href.lastIndexOf('/');
let path = window.location.href; if (index !== -1) {
path = path.slice(0, path.lastIndexOf('/') + 1); const kmlFile = zip.file(href.slice(index + 1));
if (href.startsWith(path)) {
const regexp = new RegExp(href.replace(path, '') + '$', 'i');
const kmlFile = zip.file(regexp)[0];
if (kmlFile) { if (kmlFile) {
url = URL.createObjectURL(new Blob([kmlFile.asArrayBuffer()])); return URL.createObjectURL(new Blob([kmlFile.asArrayBuffer()]));
} }
} }
return url; return href;
} }
// Define a KMZ format class by subclassing ol/format/KML // Define a KMZ format class by subclassing ol/format/KML