替换 GeoJSON 中的子字符串以弹出 window Leaflet

Replace substring in GeoJSON for popup window Leaflet

已经研究了很多关于替换子字符串的问题,但是 none 个示例对我有用。我在 geojson (feature.properties.openingHours) 中有一个字符串,这个属性有“周一、周五、周六、周日”以及时间“00:00-24:00 周一至周日”等。我需要将工作日更改为当在 Leaflet 地图中的某个点上发生“点击”时,弹出窗口的本地语言。该字符串可以包含多个开放时间,例如“00:00-20:00 Mon-Fri 08:00-20:00 Sat 08:00-18:00 Sun”

这是我最近尝试过的,但我收到错误“未定义天数”,这是错误的做法,还是缺少什​​么?

结果应为“00:00-24:00 Mon-Sun”=>“00:00-24:00 Mån-Sön”

    function openingHours(feature, latlng){ 
    var days = feature.properties.openingHours
        days = days.replace(/Mon/g, 'Mån')
        days = days.replace(/Fri/g, 'Fre')
        days = days.replace(/Sat/g, 'Lör')
        days = days.replace(/Sun/g, 'Sön');
    };

geoLayer = L.geoJson(json, {
     
    pointToLayer: function (feature, latlng) {
    return new L.shapeMarker(latlng, {radius: 7, color: '#b30000', fillOpacity: 0.7, weight: 2,  shape: 'square'})
    },

onEachFeature: function(feature, layer) {
    var popupText = '';
    popupText += (feature.properties.openingHours) ? '<br></b><i>Öppettider:</i>': '';
    popupText += (feature.properties.openingHours) ? '<br></b><i>' + openingHours(days) + '</i><br>': '';

您向 openingHours 函数传递了一个未定义的变量。

将您的代码更改为:

   function openingHours(feature){ 
    var days = feature.properties.openingHours
        days = days.replace(/Mon/g, 'Mån')
        days = days.replace(/Fri/g, 'Fre')
        days = days.replace(/Sat/g, 'Lör')
        days = days.replace(/Sun/g, 'Sön');
        return days;
    };

geoLayer = L.geoJson(json, {
    pointToLayer: function (feature, latlng) {
         return new L.shapeMarker(latlng, {radius: 7, color: '#b30000', fillOpacity: 0.7, weight: 2,  shape: 'square'})
    },
    onEachFeature: function(feature, layer) {
        var popupText = '';
        popupText += (feature.properties.openingHours) ? '<br></b><i>Öppettider:</i>': '';
        popupText += (feature.properties.openingHours) ? '<br></b><i>' + openingHours(feature) + '</i><br>': '';
    }
});