2016-06-20 111 views
2

我正在嘗試將標記添加到leaflet.indoor。我從geoJson文件中獲取數據。我一直得到這個錯誤「Uncaught TypeError:無法讀取未定義的屬性'addLayer'leaflet-indoor.js:57。我相對較新的JavaScript和編程。Uncaught TypeError:無法讀取未定義的屬性'addLayer' - 小冊子

這是我的代碼。

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

回答

0

//我試圖用

this.map.on( '點擊',this.onMapClick);

//失敗

//我改爲

this.map.on( '點擊',(E)=> {this.onMapClick(E)});

//這項工作,不要問我爲什麼

+2

你能提供更多關於這與問題相關的細節嗎?我在OP中的任何地方都沒有看到'onMapClick',或者任何顯示點擊地圖的指示都是問題或與解決方案有關。 – GalacticCowboy

相關問題