猿问

不断得到“未定义”,但是我尝试用 javascript 解析 geojson 文件

我有这个 geojson 文件:


{

   "type":"FeatureCollection",

   "features":[

      {

         "type":"Feature",

         "properties":{

            "Adresse":"### St-Roch",

            "Type":"famille"

         },

         "geometry":{

            "type":"Point",

            "coordinates":[

               -73.6291292309761,

               45.52982413033413

            ]

         }

      },

      {

         "type":"Feature",

         "properties":{

            "Adresse":"### St-Roch",

            "Type":"organisme"

         },

         "geometry":{

            "type":"Point",

            "coordinates":[

               -73.62943768501282,

               45.531770729329985

            ]

         }

      }

   ]

}

这是我的脚本(的一部分):


        var request = new XMLHttpRequest();

        request.open("GET", "photoParcEx.geojson", false);

        request.send(null);

        var dataJSON = JSON.parse(request.responseText);

        console.log(dataJSON.features.properties.Adresse);

        

        photoParcEx.addTo(maCarte).on('click', function () {

            sidebar.toggle();

            document.getElementById("sidebar").innerHTML = "Type : " + dataJSON.Adresse;

        });

我试图将“Adresse”键发送到内部 HTML,但始终未定义。当我尝试使用控制台日志打印该值时,情况也是如此。


我究竟做错了什么 ?


摇曳的蔷薇
浏览 101回答 1
1回答

守候你守候我

这做到了这一点:photoParcEx.on('click', function (e) {&nbsp; &nbsp; &nbsp; sidebar.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(e.layer.feature.properties.Type);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var photoDesc = document.getElementById("photoDesc");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //$("sidebar").innerHTML = "Type : " + e.layer.properties.Type;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (photoDesc.style.display === 'none') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoDesc.style.display = "block";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("type").innerHTML = "TYPE : " + (e.layer.feature.properties.Type).toUpperCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("desc").innerHTML = "<b>MY STORY : </b>" + e.layer.feature.properties.description;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("photo").src = e.layer.feature.properties.image1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeBanner();&nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答