使用Leaflet

时间:2019-05-24 21:12:52

标签: javascript leaflet

带点的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();
      });
    },

geonjson文件的一部分

{ “ type”:“ FeatureCollection”, “ name”:“数据”, “ crs”:{“ type”:“ name”,“ properties”:{“ name”:“ urn:ogc:def:crs:OGC:1.3:CRS84”}}, “特征”:[ {“ type”:“功能”,“属性”:{“ X”:“ NORRKOPING”,“ B”:60208,“ Y”:“ NORRKOPING”,“ CODE”:“ 60208A”,“ ID”:2, “ NAME”:“ OSCAR”,“ LOAD_1”:0,“ LOAD_2”:4},“ geometry”:{“ type”:“ Point”,“ coordinates”:[16.150801,58.608192]}},

单击cirlceMarker时,弹出窗口应以字符串值显示工作日,而不是以数字显示工作日。但是在这个例子中,我将在弹出窗口中显示完整的工作日代码。

1 个答案:

答案 0 :(得分:2)

您没有正确调用函数:

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';
   }
}