未捕获的TypeError:无法读取undefined - leaflet的属性'addLayer'

时间:2016-06-20 22:22:03

标签: javascript jquery leaflet geojson

我正在尝试将标记添加到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;
    }

1 个答案:

答案 0 :(得分:0)

//我尝试使用

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

//失败

//我改为

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

//那个工作,不要问我为什么