2015-04-23 38 views
2

我目前正在使用外部geojson文件作为数据输入的Leaflet项目。由于JSON中包含了大量的对象,我想用MarkerCluster插件,这是我从Mappbox有:传单MarkerCluster与GeoJson

<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/leaflet.markercluster.js'></script> 
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css' rel='stylesheet' /> 
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css' rel='stylesheet' /> 

显示JSON-层不聚类工作得很好,但如果我尝试把它分配给了群集什么都没有显示。

var markersBar = L.markerClusterGroup();   
var barLayer = new L.GeoJSON.AJAX("json/eat_drink/bar.geojson", { 
    pointToLayer: function(feature, latlng) { 
     var icon = L.icon({ 
         iconSize: [27, 27], 
         iconAnchor: [13, 27], 
         popupAnchor: [1, -24], 
         iconUrl: 'icon/' + feature.properties.amenity + '.png' 
         }); 
     return L.marker(latlng, {icon: icon}) 
    }, 
    onEachFeature: function(feature, layer) { 
     layer.bindPopup(feature.properties.name + ': ' + feature.properties.opening_hours); 
    } 
}); 
markersBar.addLayer(barLayer); 
console.log(markersBar); 
map.addLayer(markersBar); 

console.log输出让我假设没有对象,但我不明白为什么。

Object { options: Object, _featureGroup: Object, _leaflet_id: 24, _nonPointGroup: Object, _inZoomAnimation: 0, _needsClustering: Array[0], _needsRemoving: Array[0], _currentShownBounds: null, _queue: Array[0], _initHooksCalled: true } 

我在做什么错?

回答

3

好吧,它看起来像你使用Leaflet-Ajax ...所以一个异步请求,以获取您的geojson..and你的直接下一行是markersBar.addLayer(barLayer); ..哪些不包含任何东西,因为请求几乎肯定还没有完成。 ..

相反,我相信你可以使用在documentation提供的加载事件像

barLayer.on('data:loaded', function() { 
    markersBar.addLayer(barLayer); 
    console.log(markersBar); 
    map.addLayer(markersBar); 
}); 
+0

哇,那没有的伎俩!非常感谢@snkashis – eltomaco

+0

这工作,但脚本缺少a);在最后。 – MarsAndBack