Adding a satellite image as KMZ file to openlayers – Image not shown

I am trying to add one or more satellite images for small regions, which have been tiled and packed into a KMZ file, to Openlayers 5. I manage to download the KMZ file from the server and unzip it. Then I read the KML file within. However, the image itself is never shown and the function ‘iconUrlFunction’ is never used. Could someone help me with my code?

My current approach looks like this (I add KMZ only if files on server are available):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if(lConfig['kmzFiles']!=null && typeof lConfig['kmzFiles']!="undefined" && lConfig['kmzFiles'].length > 0) {
let urls = ['https://stuk.github.io/jszip/dist/jszip.min.js','https://stuk.github.io/jszip-utils/dist/jszip-utils.min.js'];
let loadedScripts=0;
//adding required scripts to DOM
urls.forEach(url => {
let script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.onload = function() {
//console.log(url + ' has been loaded');
loadedScripts++;
//run code only once all scripts are loaded
if (loadedScripts === urls.length) {
//adding new group to which KMZ layers are added
let group = new ol.layer.Group({
title: 'Background Images',
fold:'close',
visible:false,
layers:[]
});
lMap.addLayer(group);
//function for loading KMZ files and add the to Openlayers
createKmz_Layers(group, lConfig);
}
};
script.onerror = function() {
console.error('Error loading KMZ script: ' + url);
};
document.head.appendChild(script);
});
}
//class for KMZ files. Taken from https://openlayers.org/en/latest/examples/drag-and-drop-custom-kmz.html
class KMZ extends ol.format.KML {
constructor(opt_options,zip, kmlData) {
const options = opt_options || {};
//this function is somehow never used ?????????????????
options.iconUrlFunction = function(href) {
const index = window.location.href.lastIndexOf('/');
if (index !== -1) {
const kmlFile = zip.file(href.slice(index + 1));
if (kmlFile) {
return URL.createObjectURL(new Blob([kmlFile.asArrayBuffer()]));
}
}
return href;
};
super(options);
this.zip=zip;
this.kmlData=kmlData;
}
getType() {
return 'arraybuffer';
}
readFeature(source, options) {
return super.readFeature(this.kmlData, options);
}
readFeatures(source, options) {
return super.readFeatures(this.kmlData, options);
}
}
function createKmz_Layers(group, pConfig) {
for(let i = 0; i < pConfig['kmzFiles'].length;i++) {
let kmzUrl = 'https://...';
//Loading KMZ from server
JSZipUtils.getBinaryContent(kmzUrl, function(err, data) {
if (err) {
console.log('Error while reading KMZ file', err);
}
//reading/unzip KMZ
JSZip.loadAsync(data).then(function(zip) {
const kmlFile = zip.file(/.kml$/i)[0];
if (kmlFile) {
//read KML file data from KMZ (ZIP)
kmlFile.async("string").then(function (content) {
//create new layer with KMZ class as format
let layer = new ol.layer.Vector({
title:lLayerName,
source: new ol.source.Vector({
url: kmzUrl,
format: new KMZ({},zip,content)
}),
visible:false
});
//setting z index to show on top of OSM
layer.setZIndex(99);
//adding layers to group dynamically
if (group.getLayers){
let existingLayers = group.getLayers();
existingLayers.push(layer);
if (existingLayers instanceof ol.Collection){
group.setLayers(existingLayers);
}
}
});
}
});
});
}
}
</code>
<code>if(lConfig['kmzFiles']!=null && typeof lConfig['kmzFiles']!="undefined" && lConfig['kmzFiles'].length > 0) { let urls = ['https://stuk.github.io/jszip/dist/jszip.min.js','https://stuk.github.io/jszip-utils/dist/jszip-utils.min.js']; let loadedScripts=0; //adding required scripts to DOM urls.forEach(url => { let script = document.createElement('script'); script.src = url; script.type = 'text/javascript'; script.onload = function() { //console.log(url + ' has been loaded'); loadedScripts++; //run code only once all scripts are loaded if (loadedScripts === urls.length) { //adding new group to which KMZ layers are added let group = new ol.layer.Group({ title: 'Background Images', fold:'close', visible:false, layers:[] }); lMap.addLayer(group); //function for loading KMZ files and add the to Openlayers createKmz_Layers(group, lConfig); } }; script.onerror = function() { console.error('Error loading KMZ script: ' + url); }; document.head.appendChild(script); }); } //class for KMZ files. Taken from https://openlayers.org/en/latest/examples/drag-and-drop-custom-kmz.html class KMZ extends ol.format.KML { constructor(opt_options,zip, kmlData) { const options = opt_options || {}; //this function is somehow never used ????????????????? options.iconUrlFunction = function(href) { const index = window.location.href.lastIndexOf('/'); if (index !== -1) { const kmlFile = zip.file(href.slice(index + 1)); if (kmlFile) { return URL.createObjectURL(new Blob([kmlFile.asArrayBuffer()])); } } return href; }; super(options); this.zip=zip; this.kmlData=kmlData; } getType() { return 'arraybuffer'; } readFeature(source, options) { return super.readFeature(this.kmlData, options); } readFeatures(source, options) { return super.readFeatures(this.kmlData, options); } } function createKmz_Layers(group, pConfig) { for(let i = 0; i < pConfig['kmzFiles'].length;i++) { let kmzUrl = 'https://...'; //Loading KMZ from server JSZipUtils.getBinaryContent(kmzUrl, function(err, data) { if (err) { console.log('Error while reading KMZ file', err); } //reading/unzip KMZ JSZip.loadAsync(data).then(function(zip) { const kmlFile = zip.file(/.kml$/i)[0]; if (kmlFile) { //read KML file data from KMZ (ZIP) kmlFile.async("string").then(function (content) { //create new layer with KMZ class as format let layer = new ol.layer.Vector({ title:lLayerName, source: new ol.source.Vector({ url: kmzUrl, format: new KMZ({},zip,content) }), visible:false }); //setting z index to show on top of OSM layer.setZIndex(99); //adding layers to group dynamically if (group.getLayers){ let existingLayers = group.getLayers(); existingLayers.push(layer); if (existingLayers instanceof ol.Collection){ group.setLayers(existingLayers); } } }); } }); }); } } </code>
if(lConfig['kmzFiles']!=null && typeof lConfig['kmzFiles']!="undefined" && lConfig['kmzFiles'].length > 0) {
    let urls = ['https://stuk.github.io/jszip/dist/jszip.min.js','https://stuk.github.io/jszip-utils/dist/jszip-utils.min.js'];
    let loadedScripts=0;
    //adding required scripts to DOM
    urls.forEach(url => {
        let script = document.createElement('script');
        script.src = url;
        script.type = 'text/javascript';

        script.onload = function() {
            //console.log(url + ' has been loaded');
            loadedScripts++;
            //run code only once all scripts are loaded
            if (loadedScripts === urls.length) {
                //adding new group to which KMZ layers are added
                let group = new ol.layer.Group({
                        title: 'Background Images',
                        fold:'close',
                        visible:false,
                        layers:[]
                    });
                lMap.addLayer(group);
                //function for loading KMZ files and add the to Openlayers
                createKmz_Layers(group, lConfig);
            }
        };
        script.onerror = function() {
            console.error('Error loading KMZ script: ' + url);
        };

        document.head.appendChild(script);
    });
}

//class for KMZ files. Taken from https://openlayers.org/en/latest/examples/drag-and-drop-custom-kmz.html
class KMZ extends ol.format.KML {
  constructor(opt_options,zip, kmlData) {
    const options = opt_options || {};
    //this function is somehow never used ?????????????????
    options.iconUrlFunction = function(href) {
          const index = window.location.href.lastIndexOf('/');
          if (index !== -1) {
            const kmlFile = zip.file(href.slice(index + 1));
            if (kmlFile) {
              return URL.createObjectURL(new Blob([kmlFile.asArrayBuffer()]));
            }
          }
          return href;
        };
    super(options);
    this.zip=zip;
    this.kmlData=kmlData;
  }

  getType() {
    return 'arraybuffer';
  }

  readFeature(source, options) {
    return super.readFeature(this.kmlData, options);
  }

  readFeatures(source, options) {
    return super.readFeatures(this.kmlData, options);
  }
}

function createKmz_Layers(group, pConfig) {
    
    for(let i = 0; i < pConfig['kmzFiles'].length;i++) {        
        let kmzUrl = 'https://...';
        //Loading KMZ from server
        JSZipUtils.getBinaryContent(kmzUrl, function(err, data) {
            if (err) {
                console.log('Error while reading KMZ file', err);
            }
            //reading/unzip KMZ
            JSZip.loadAsync(data).then(function(zip) {
                  const kmlFile = zip.file(/.kml$/i)[0];
                  if (kmlFile) {
                    //read KML file data from KMZ (ZIP)
                      kmlFile.async("string").then(function (content) {
                            //create new layer with KMZ class as format
                            let layer = new ol.layer.Vector({
                            title:lLayerName,
                            source: new ol.source.Vector({
                              url: kmzUrl,
                              format: new KMZ({},zip,content)
                            }),
                            visible:false
                        });
                        //setting z index to show on top of OSM
                        layer.setZIndex(99);
                        //adding layers to group dynamically
                        if (group.getLayers){
                             let existingLayers = group.getLayers(); 
                             existingLayers.push(layer); 
                             if (existingLayers instanceof ol.Collection){
                               group.setLayers(existingLayers);
                             }
                         }
                      });
                  }
            });
        });
    }
}

Basically, the code runs fine but the image is never shown and I don’t know what to change.

In the KMZ, there exists one .kml file and the rest are .png files with the tiled images.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật