该映射使用KML文件生成一个geoJSON对象,以传递给VectorGrid的切片器函数。为了提高性能,文件作为一个KMZ,并使用JSZip库进行解压缩。然后循环遍历每个文件(KML),解析它并转换为geoJSON。这些特性被连接到一个单独的数组上,该数组用于创建最终的geoJSON对象(一种廉价的合并方式)。
var vectorGrid;
JSZipUtils.getBinaryContent('/pathto/file.kmz', function (error, data) {
JSZip.loadAsync(data).then(function (zip) {
var featureArray = [];
zip.forEach(function (path, file) {
file.async('string').then(function (data) {
// convert to geoJSON, concatenate features array
featureArray = featureArray.concat(geoJSON.features);
}
}
var consolidatedGeoJSON = {
'type': 'FeatureCollection,
'features': featureArray
};
vectorGrid = L.vectorGrid.slicer(consolidatedGeoJSON, options);
}
}这个想法是,一旦操作完成,我就可以获得最终的geoJSON,并简单地将它传递给切割机。但是,由于承诺的性质,它总是先构造切片程序,然后再解析文件。
为了解决这个问题,我不得不将切片器函数放在forEach中,但在if语句中检查当前文件是否是zip中的最后一个文件。这允许在地图上绘制向量,但现在我不能分别在每个层上启用悬停效果(每个KML包含一个用作交互区域轮廓的特定层)。
vectorGrid滑块选项允许您指定一个getFeatureId函数,但我不知道如何在事件处理程序中将该id传递给setFeatureStyle函数。
发布于 2018-03-14 08:09:31
基本问题是,在将值赋值给vactorGrid之前,要先将值赋值给featureArray。我认为你需要使用Promise.all(..)。就像这样:
var zips=[];
zip.forEach(function(path,file) {
zips.push(file.async('string');
});
Promise.all(zips).then(function(data){
return data.map(function(value){
return value.features;
});
}).then(function(featureArray) {
vectorGrid = L.vectorGrid.slicer(
{type:'FeatureCollection',feature:featureArray}, options);
});https://stackoverflow.com/questions/49271342
复制相似问题