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