Neo Mareka
Neo Mareka

Reputation: 21

Uncaught TypeError: Cannot read property 'addLayer' of undefined - leaflet

I'm trying to add markers to leaflet.indoor. I am getting my data from a geoJson file. I keep getting this error "Uncaught TypeError: Cannot read property 'addLayer' of undefined" leaflet-indoor.js:57 . i am relatively new to javascript and programming.

here is my code.

$.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);
});

i edited the part of the code as show below, but i still get no labels.

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;
    }

Upvotes: 2

Views: 3928

Answers (1)

Jaume
Jaume

Reputation: 41

//I tried to use

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

//that failed

//I changed to

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

//And that work's, don't ask me why

Upvotes: 4

Related Questions