我正在尝试传递两个函数作为参数,以便为Leaflet贴图自定义图层可视化。除了特定情况外,在我要求更笼统的概念时,这可能与此处无关,我所做的是:
Map.createLayer(function onEachFeature(feature, layer) {
var popupContent = "<p>I started out as a GeoJSON " +
feature.geometry.type + ", but now I'm a Leaflet vector!</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
},
function pointToLayer(feature, latlng) {
return L.circleMarker(latlng, {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
})
});
Map(包装Leaflet Map)的方法“ createLayer”在其中执行以下操作:
public createLayer(popupContent:(feature, layer) => void, renderPoint:(feature,latlng) => L.CircleMarker): ICOLayer{
this.layer = L.geoJSON(this.datasource.get(), {
style: function(feature) {
return feature.properties && feature.properties.style;
},
onEachFeature: popupContent,
pointToLayer: renderPoint
});
return this;
}
这不起作用,我也不知道为什么,但是如果我将它们作为匿名函数传递(从而忽略了输入参数),那么一切都可以正常工作
public createLayer(popupContent:(feature, layer) => void, renderPoint:(feature,latlng) => L.CircleMarker): ICOLayer{
this.layer = L.geoJSON(this.datasource.get(), {
style: function(feature) {
return feature.properties && feature.properties.style;
},
onEachFeature: function onEachFeature(feature, layer) {
var popupContent = "<p>I started out as a GeoJSON " +
feature.geometry.type + ", but now I'm a Leaflet vector!</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
},
pointToLayer: function pointToLayer(feature, latlng) {
return L.circleMarker(latlng, {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
})
}
});
return this;
}
我也尝试过删除输入中函数的签名,将它们作为纯变量传递,但这没有用, 我不熟悉javascript / typescript,因此这可能是一个愚蠢的错误。请原谅我。
编辑:执行第一种方法时未显示错误消息