问题描述
我尝试在地图上显示多个 GPX 轨迹,并使用一些自定义数据将单个弹出窗口绑定到它们。但我不确定如何将诸如 Id 或 Description 之类的自定义属性传递给 onEachFeature
用于加载具有某些属性和 GPX 文件路径的 JSON 的代码:
var tours = JSON.parse('{"tourdata":[{"tourid" : 1,"desc": "nice tour 1","gpxFileName" : "./1.gpx"},{"tourid" : 3,"desc": "nice tour 2","gpxFileName" : "./3.gpx"}]}');
for (var i = 0; i < tours.tourdata.length; i++) {
var tour = tours.tourdata[i];
var gpxLayer = omnivore.gpx(tour.gpxFileName,null,customLayer).addTo(map);
/*
how can I pass here the tour.desc and tour.id?
gpxLayer.properties.desc = tour.desc; // doesn't work
*/
}
自定义图层:
var customLayer = L.geoJson(null,{
style: function (feature) {
return { color: '#f00',weight: 2 };
},onEachFeature: onEachGpxFeature
});
onEachGpxFeature:
function onEachGpxFeature(feature,layer) {
/* how to access custom properties here? */
var popupContent = "<p>";
if (feature.properties) {
if(feature.properties.name) {
popupContent += "<br /><b>Name:</b>" + feature.properties.name;
}
if(feature.properties.desc) {
popupContent += "<br /><b>Description:</b>" + feature.properties.desc;
}
}
popupContent += "</p>";
layer.bindPopup(popupContent);
}
我只能访问由杂食动物解析的标准 GPX 属性。
这是我的完整演示代码:https://plnkr.co/edit/v0BeshnKSx6sPqA1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)