我想问一问,在这段代码中省略eval ()函数是否合理。特别是
<script>
...
...
function addGeoJson (geoJsonPath, iconPath = "leaflet-2/images/marker-icon.png", iconSize = [30,50], popUpContent, sideBarContent)
{$.getJSON(geoJsonPath,function(data){
var geoIcon = L.icon({
iconUrl: iconPath,
iconSize: iconSize
});
L.geoJson(data,{
pointToLayer: function(feature,latlng){
var marker = L.marker(latlng,{icon: geoIcon, riseOnHover: true});
marker.bindPopup(eval(popUpContent));
marker.on('mouseover',function() {
document.getElementById('sidebar').innerHTML = eval(sideBarContent);
});
marker.on('mouseout', function(){
document.getElementById('sidebar').innerHTML = " ";
});
return marker;
}
}).addTo(map);
});
};
var layer1pop = "feature.properties.name + '<br/>' + feature.properties.info";
var layer1side = "feature.properties.price + '<br/>' + feature.properties.web";
addGeoJson("points.geojson", undefined, undefined, layer1pop, layer1side);
...
...
</script>我想用多个addGeoJson文件重用geoJSON函数,并且每个文件都有不同的标记、弹出、侧边栏等模板。在我看来,切换似乎不够,以防我不知道geoJSON可以拥有多少属性,如果我需要在弹出窗口中更改属性项的顺序,例如,我可以只编辑addGeoJson基金的参数。如果我试图通过,会出现一个错误。
var layer1pop = feature.properties.name + '<br/>' + feature.properties.info;
var layer1side = feature.properties.price + '<br/>' + feature.properties.web;直接因为不可能到达本地feature,并且在传递参数时甚至不存在。我试图用new Function替换它,但也无法联系到feature。
你有什么想法吗?谢谢。
发布于 2021-06-05 15:00:11
为什么不将行marker.bindPopup(eval(popUpContent));改为marker.bindPopup(feature.properties.name + '<br/>' + feature.properties.info);
并将相同的逻辑应用于第二个函数。
如果有什么东西阻止了你,就重写它,让它起作用。
伊瓦尔是个恶毒的暗号
如果映射不断变化(即您将使用的feature的哪个部分),则尝试传递一个知道要提取什么的函数
var layer1pop = function(feature){
return feature.properties.name + '<br/>' + feature.properties.info;
}然后marker.bindPopup(eval(popUpContent));变成marker.bindPopup(popUpContent(feature));
https://stackoverflow.com/questions/67850297
复制相似问题