我在单个FeatureCollection中具有Point,MultiPolygon和Linestring功能。我想用circleMarker()设置点的样式,而其他人则通常这样。我将点添加到idToFeature对象以使用JavaScript进行操作,但无法添加非点功能。
使用Leaflet 1.3.3,如果我使用pointToLayer :,则点的样式很好,而其他类型的确渲染,默认为蓝色(不知道为什么,因为它们不是点)。我尝试使用onEachFeature:代替,并且可以用这种方式设置非点的样式,但无法弄清楚如何为点生成circleMarkers。
我拥有的 (无法设置MultiPolygon的样式,或将其添加到我的idToFeature对象中以进行JS操作)
// geom is an array of features inc. Point, MultiPolygon, Linestring
renderPlaces = function(geoms) {
data = {"type":"FeatureCollection","features":geoms}
idToFeature = {}
mappy.createPane('placePane');
mappy.getPane('placePane').style.zIndex = 200;
features = L.geoJSON(data, {
pane: 'placePane',
pointToLayer: function (feature, latlng) {
identifier = feature.properties.id;
if(feature.type=='Point'){
marker = L.circleMarker(latlng, styles.place_default)
.bindPopup(feature.properties.title+' (id:'+identifier+')');
// add to array for programmatic selection
idToFeature[identifier] = marker
return marker
}
}
}).addTo(map);
}
我尝试过的事情(无论如何都可以尝试)
features = L.geoJSON(data, {
style: function (feature) {
if(feature.type=="Point"){
return {color: "#009900"};
} else {
return {color: "#000099"};
}
}
onEachFeature: function(feature, layer) {
if(feature.type == 'Point'){
console.log('point', feature)
marker = L.circleMarker(latlng, styles.place_default)
return marker
}
}
}).addTo(map);
我不想将GeoJSON分解为单独的特定于类型的FeatureCollection,我觉得我不必...但是如何?
答案 0 :(得分:0)
我拼凑了一个解决方案。它似乎并不理想,但可以工作:依次使用pointToLayer
和onEachFeature
。
features = L.geoJSON(data, {
pointToLayer: function (feature, latlng) {
count_features +=1;
identifier = feature.properties.whgid;
if(feature.type=='Point'){
marker = L.circleMarker(latlng, styles.marker_default
).bindPopup(feature.properties.title+' (whg id:'+identifier+')');
// add to array for selection
idToFeature[identifier] = marker
return marker
}
},
onEachFeature: function(feature,layer) {
identifier = feature.properties.whgid;
if(feature.type != 'Point'){
layer.setStyle(styles.default)
idToFeature[identifier] = layer
}
}
}).addTo(map);