【发布时间】:2016-06-20 22:22:02
【问题描述】:
我正在尝试向 leaflet.indoor 添加标记。我从 geoJson 文件中获取数据。我不断收到此错误“Uncaught TypeError: Cannot read property 'addLayer' of undefined”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;
}
【问题讨论】:
-
嘿,我知道那是几年前的事了,但我也面临同样的问题。它让我发疯。你解决了吗?
标签: javascript jquery leaflet geojson