带有点的geojson文件具有整数值,需要在弹出窗口中将其转换为字符串值以便用户可读。尝试使用函数但无法使其正常工作。有任何建议来纠正此代码或其他方法来解决此问题吗?
function weekday(feature, layer){
switch (feature.properties.ID){
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
}
}
$.getJSON("../data/abc123/data.geojson", function(json) {
geoLayer = L.geoJson(json, {
onEachFeature: function(feature, layer) {
var popupText =
"Data: <b>GPS log</b>" +
"<br><b>Startingpoint</b>: " + feature.properties.X +
"<br><b>Endpoint</b>: " + feature.properties.Y +
"<br><b>Weekday</b>: " + (feature.properties.ID, weekday)
layer.bindPopup(popupText, {
closeButton: true,
offset: L.point(0, -20)
});
layer.on('click', function() {
layer.openPopup();
});
},
geojson 文件的一部分
{ "type":"特征集合", “名称”:“数据”, "crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}, “特征”:[ {“类型”:“功能”,“属性”:{“X”:“NORRKOPING”,“B”:60208,“Y”:“NORRKOPING”,“代码”:“60208A”,“ID”:2, "NAME":"OSCAR","LOAD_1":0,"LOAD_2":4},"几何":{"type":"点","坐标":[16.150801,58.608192]}},
当单击 cirlceMarker 时,弹出窗口应以字符串值显示工作日,而不是数字形式的工作日。但通过这个例子,我得到了在弹出窗口中显示的完整函数工作日代码。
最佳答案
您没有正确调用您的函数:
weekday(feature.properties.ID)
由于您已经将 ID 作为参数传递,因此您不需要整个功能,也不需要参数中的图层,并且您可以按如下方式定义函数:
function weekday(ID){
switch (ID){
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
}
}
关于javascript - 使用 Leaflet 中的弹出窗口将整数更改为字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299592/