gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'addLayer' of undefined - leaflet

转载 作者:行者123 更新时间:2023-12-01 05:30:24 32 4
gpt4 key购买 nike

我正在尝试向 leaflet.indoor 添加标记。我正在从 geoJson 文件获取数据。我不断收到此错误“Uncaught TypeError:无法读取未定义的属性'addLayer'” leaflet-indoor.js:57 。我对 JavaScript 和编程还比较陌生。

这是我的代码。

$.getJSON("gateway.json", function(geoJSON) {

var indoorLayer = new L.Indoor(gateway, {
getLevel: function(feature) {
if (feature.properties.length === 0)
return null;

return feature.properties.level;
},

markerForFeature: function(feature) {

var content = feature.properties.name;
var iconCoords = feature.properties.center;
var myIcon = L.divIcon({
className: 'ls-room-marker',
html: content,
iconSize: new L.Point(100, 14),
iconAnchor: new L.Point(50, 7)
});

var marker = L.marker(iconCoords, {
icon: myIcon
});

return marker;
},

onEachFeature: function(feature, layer) {
layer.bindPopup(JSON.stringify(feature.properties, null, 2));

},

style: function(feature) {
var fill = 'white';

if (feature.properties.buildingpart === 'base') {
fill = 'silver';
} else if (feature.properties.buildingpart === 'hallway') {
fill = 'cornsilk';
}

return {
fillColor: fill,
weight: 1,
color: '#666',
fillOpacity: 1
};
}

});

indoorLayer.setLevel("0");

indoorLayer.addTo(map);

var levelControl = new L.Control.Level({
level: "0",
levels: indoorLayer.getLevels()
});

// Connect the level control to the indoor layer
levelControl.addEventListener("levelchange", indoorLayer.setLevel, indoorLayer);

levelControl.addTo(map);
});

我编辑了部分代码,如下所示,但仍然没有标签。

markerForFeature: function(feature) {
if (feature.properties.category === "general"){
var iconCoords = feature.properties.center;
var content;
if ("name" in feature.properties && "category" in feature.properties) {
content = feature.properties.name + "(" + feature.properties.category + ")";
}
else if
("category" in feature.properties) {
content = feature.properties.category;
} else if ("name" in feature.properties) {
content = feature.properties.name;
} else {
return;
}

最佳答案

//我尝试使用

this.map.on('click', this.onMapClick);

//失败了

//我改为

this.map.on('click', (e)=> {this.onMapClick(e)});

//那行得通,别问我为什么

关于javascript - 未捕获的类型错误 : Cannot read property 'addLayer' of undefined - leaflet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37932765/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com