Search code examples
javascriptgeojsongetelementbyid

Keep getting "undefined" however I try to parse a geojson file with javascript


I have this geojson file:

{
   "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
            ]
         }
      }
   ]
}

This is (part of) my script:

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

I'm trying to send the "Adresse" key to an inner HTML but keep getting undefined. Same thing when I try to print the value with the console log.

What am I doing wrong ?


Solution

  • This did the trick:

    photoParcEx.on('click', function (e) {
          sidebar.show();
                console.log(e.layer.feature.properties.Type);
                var photoDesc = document.getElementById("photoDesc");
                //$("sidebar").innerHTML = "Type : " + e.layer.properties.Type;
                if (photoDesc.style.display === 'none') {
                    photoDesc.style.display = "block";
                  } else {
                  } 
                document.getElementById("type").innerHTML = "TYPE : " + (e.layer.feature.properties.Type).toUpperCase();
                document.getElementById("desc").innerHTML = "<b>MY STORY : </b>" + e.layer.feature.properties.description;
                document.getElementById("photo").src = e.layer.feature.properties.image1;
                removeBanner();
        });