gpt4 book ai didi

javascript - Leaflet 多 geoJSON 层聚类和层控制

转载 作者:行者123 更新时间:2023-12-03 04:23:07 25 4
gpt4 key购买 nike

我想为多个 geoJSON 层实现标记聚类。经过一番研究,使用 Leaflet.FeatureGroup.SubGroup插件看起来像是我需要的解决方案。

如何使用我提供的当前代码示例迭代 geoJSON 以动态添加/删除 map 中的图层组?

addGeoJsonLayers: =>
L.mapbox.accessToken = MAP_BOX_ACCESS_TOKEN

streetsLayer = L.mapbox.tileLayer('mapbox.streets')
streetsLayer.on 'ready', => streetsLayer.addTo(@map)

baseLayers =
'Road': streetsLayer
'Terrain': L.mapbox.tileLayer('mapbox.outdoors')
'Satellite': L.mapbox.tileLayer('mapbox.satellite')

geojsonLayers = {}

@shares.forEach (share, index) =>
titleLabel = CommunityMap.labelForDataId(share.schema.elements, share.schema.record_title_key)
geojsonOptions = {
onEachFeature: (feature, layer) =>
record = new CommunityGeoJSONRecord(properties: feature.properties, titleLabel: titleLabel)

@records[record.id] = { record: record, layer: layer }

layer.bindPopup record.contentForPopUp()

pointToLayer: (feature, latLng) ->
if share.status_enabled
color = feature.properties['marker-color'] || INVALID_STATUS_MARKER_COLOR
else
color = MARKER_COLORS[index]
L.marker([latLng.lat, latLng.lng], {
icon: L.mapbox.marker.icon({
'marker-size': 'small'
'marker-color': color
})
})
}

share.geojsonLayer = L.geoJson(null, geojsonOptions).addTo(@map)

geojsonLayers[share.name] = share.geojsonLayer

$.getJSON "#{share.url}?human_friendly=1", (data) =>
@loadData(data, share.geojsonLayer, titleLabel)

L.control.zoom(position: 'topright').addTo(@map)

L.control.layers(baseLayers, geojsonLayers, position: 'topright').addTo(@map)

最佳答案

类似的东西应该有效:

var mcg = L.markerClusterGroup().addTo(@map);

// within your @shares loop…

share.subGroupLayer = L.featureGroup.subGroup(mcg).addTo(@map);

// Do not add directly to map, but to the intermediate subGroup instead!
share.geojsonLayer = L.geoJSON(null, geojsonOptions).addTo(share.subGroupLayer);

geojsonLayers[share.name] = share.subGroupLayer; // use the subgroup instead.

关于javascript - Leaflet 多 geoJSON 层聚类和层控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43857152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com