【问题标题】:Leaflet MarkerCluster with GeoJson带有 GeoJson 的传单 MarkerCluster
【发布时间】:2015-04-23 13:10:45
【问题描述】:

我目前正在做一个传单项目,我使用外部 geojson 文件作为数据输入。由于 json 包含很多对象,我想使用从 Mappbox 获得的 MarkerCluster 插件:

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

我做错了什么?

【问题讨论】:

    标签: json leaflet markerclusterer


    【解决方案1】:

    看来您正在使用Leaflet-Ajax...所以发出了一个异步请求来获取您的geojson..您的下一行是markersBar.addLayer(barLayer);..它不会包含任何内容,因为该请求几乎可以肯定不是还没完成……

    相反,我相信你可以使用documentation中提供的loaded事件像

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

    【讨论】:

    • 这可行,但是脚本缺少 );在最后。
    【解决方案2】:

    对于正在寻找一个简单示例的人来说,将带有 geojson ajax 的标记集群添加到地图、绑定弹出窗口和添加到图层控件:

    // pop-up function
    function popUp(f, l) {
        var out = [];
        if (f.properties) {
            for (key in f.properties) {
                out.push(key + ": " + f.properties[key]);
            }
            l.bindPopup(out.join("<br />"));
        }
    }
    
    // add layer to map and layer control
    function add_layer(layr, layr_name) {
        map.addLayer(layr);
        layerControl.addOverlay(layr, layr_name);
    }
    
    // fire ajax request
    var points = new L.GeoJSON.AJAX("../data/points.geojson", { onEachFeature: popUp });
    
    // create empty marker cluster group
    var markers = L.markerClusterGroup()
    
    // when geojson is loaded, add points to marker cluster group and add to map & layer control
    points.on('data:loaded', function () {
        markers.addLayer(points);
        add_layer(markers, "Point Markers")
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多