我有一张传单地图,允许用户上传数据(geojson、topojson、栅格、shapefile)并显示它。一切正常,直到我为添加/删除图层创建自定义控制层。NB://我的脚本有传单绘图插件,我发现支持绘图插件的传单版本不支持图层组控制选项很奇怪。
因此,我创建了以下函数来帮助我添加和删除图层:
function one(event) {
event.preventDefault();
if(m.hasLayer(files[0])) {
this.removeClass('selected');
m.removeLayer(files[0]);
} else {
m.addLayer(files[0]);
this.addClass('selected');
}
}
function two(event) {
event.preventDefault();
if(m.hasLayer(files[1])) {
this.removeClass('selected');
m.removeLayer(files[1]);
} else {
m.addLayer(files[1]);
this.addClass('selected');
}
}
function three(event) {
event.preventDefault();
if(m.hasLayer(files[2])) {
this.removeClass('selected');
m.removeLayer(files[2]);
} else {
m.addLayer(files[2]);
this.addClass('selected');
}
}
然后,在上传数据集文件时,预计以下代码行将为用户提供添加/删除图层的选项:
if (file.name.slice(-3) === 'zip') {
count++;
counter.push(count);
if(count == 1) {
func = one;
} else if(count == 2) {
func = two;
} else if(count == 3) {
func = three;
} else if(count == 4) {
func = four;
}
color = colors.getRandom();
var f1 = file.name.split('.');
var f = f1[0];
var ff = f;
if(f.length > 15) {
f = f.slice(0, 14);
f = f + '...';
}
files.push(ff);
var x = document.getElementById('toggle-layers');
var aTag = document.createElement('a');
aTag.setAttribute('href', '#');
aTag.class = 'nav-link';
aTag.onclick = func;
aTag.style.paddingLeft = '20px';
aTag.id = count;
aTag.innerHTML = f;
aTag.style.textDecoration = 'none';
var li = document.createElement('li');
li.class = 'nav-item';
x.appendChild(li);
li.appendChild(aTag);}
在单击生成的链接之前,一切正常。但是,图层没有切换,而是出现错误“未捕获的类型错误:无法分配给“sme_site”上的属性“_leaflet_id”:不是对象”。关于为什么会出现这种错误的任何原因?我确实在谷歌上搜索过,但没有此类错误的结果可用。