Leaflet.markercluster onclick错误 - 无法执行' appendChild' on'节点':参数1不是类型'节点'

时间:2015-12-13 01:00:09

标签: javascript leaflet markerclusterer

我试图在地图画布外点击.append一些文字时调用一个函数。 This example from the docs似乎有我想要的东西,但我还没能成功地将它应用到我的项目中。

我试过的代码:

var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  }),
  latlng = L.latLng(60, -100);

var map = L.map('map', {center: latlng, zoom: 4, layers: [tiles]});
var progress = document.getElementById('progress');
var progressBar = document.getElementById('progress-bar');

function updateProgressBar(processed, total, elapsed, layersArray) {
  if (elapsed > 1000) {
    progress.style.display = 'block';
    progressBar.style.width = Math.round(processed/total*100) + '%';
  }

  if (processed === total) {
    progress.style.display = 'none';
  }
}

var markers = L.markerClusterGroup({ chunkedLoading: true, chunkProgress: updateProgressBar });
var markerList = [];

function populate() {
  getCoordinates(function (data) {
    for (var i in data) {
      var a = data[i];
      var title = a.id;
      var marker = L.marker(L.latLng(a.lat, a.lng), {title: title});
      marker.bindPopup(title);
      markers.addLayer(marker);
      markerList.push(marker);
    }

    map.addLayer(markers);
  });
}

使用传单1.0测试版

时,点击标记时Chrome控制台出错
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
  L.Popup.L.Layer.extend._updateContent @ leaflet-src.js:4462
  L.Popup.L.Layer.extend.update @ leaflet-src.js:4373
  L.Popup.L.Layer.extend.onAdd @ leaflet-src.js:4308
  L.Layer.L.Evented.extend._layerAdd @ leaflet-src.js:2534
  L.Map.L.Evented.extend.whenReady @ leaflet-src.js:2382
  L.Map.include.addLayer @ leaflet-src.js:2558
  L.Map.include.openPopup @ leaflet-src.js:4592
  L.Layer.include.openPopup @ leaflet-src.js:4684
  L.Layer.include._openPopup @ leaflet-src.js:4746
  L.Evented.L.Class.extend.fire @ leaflet-src.js:488
  L.Map.L.Evented.extend._fireDOMEvent @ leaflet-src.js:2363
  L.Map.L.Evented.extend._handleDOMEvent @ leaflet-src.js:2331handler @ leaflet-src.js:6945

1 个答案:

答案 0 :(得分:2)

错误是由弹出窗口生成的,与您的点击事件/处理程序无关。单击标记时,弹出窗口会尝试将其内容设置为未定义的title变量。