0

I am using Leaflet and OpenStreetMap's to display a map on my page. I followed the steps to create a simple map on the page. Once I was able to generate a map, I follow the steps on creating a geojson file by going to https://geojson.io/.

The issue I am encountering is when I call the file, I get the following error: "Uncaught Error: Invalid GeoJSON object.". Not sure what I am doing wrong.

The following is the code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Interactive Map</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" />
        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
        <style>
            #map {
                height: 500px;
                width: 100%;
            }
            .leaflet-popup-content{ width: 330px !important; }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script>
            const myGeoJSONData = 'json/geomap.geojson';
            const mymap = L.map('map').setView([34.0000, -118.300], 13);
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',
                maxZoom: 18
            }).addTo(mymap);
            const laCounty = L.marker([34.05951960593492, -118.25037820245461], 13).addTo(mymap);
            laCounty.bindPopup("<b>Location Name</b><br><b>Address:</b> <br /><b>Phone:</b>").openPopup();

            mymap.on('click', function(e) { 
                L.marker(e.latlng).addTo(mymap); 
            });

            var myGeoJSONLayer = L.geoJSON(myGeoJSONData, {
            style: function(feature) {
                return {color: feature.properties.color};
            },
            onEachFeature: function(feature, layer) {
                layer.bindPopup(feature.properties.name);
            }
            }).addTo(mymap);
        </script>
    </body>
</html>

The following is the geojson:

 {
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               34.05951960593492,
               -118.25037820245461
            ]
         },
         "properties":{
            "name":"location name",
            "address":"address of location",
            "phone_number":"phone number"
         }
      }
   ]
}

1 Answer 1

2

myGeoJSONData needs to be a javascript object, not a string to the file location of your geojson file as you have currently.

Inline the geojson data within your script.

const myGeoJSONData = {
   "type": "FeatureCollection",
   "features": [
      {
         "type": "Feature",
         "geometry": {
            "type": "Point",
            "coordinates": [
               34.05951960593492,
               -118.25037820245461
            ]
         },
         "properties": {
            "name": "location name",
            "address": "address of location",
            "phone_number": "phone number"
         }
      }
   ]
}
var myGeoJSONLayer = L.geoJSON(myGeoJSONData, {
   style: function (feature) {
      return { color: feature.properties.color };
   },
   onEachFeature: function (feature, layer) {
      layer.bindPopup(feature.properties.name);
   }
}).addTo(mymap);
3
  • So I am not able to reference the file that contains the geojson? Commented Mar 14 at 22:24
  • I tried this approach and it still not displaying any of the location Commented Mar 14 at 22:47
  • Thank you for your help. Was able to rewrite to now display the markers. Commented Mar 15 at 0:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.