How to load local geojson file and display it on leaflet map in asp.net mvc?

I have a local geojson file saved in a folder. I would like to load and display the coordinates with marker on the map. I have successfully loaded the map on my html but i couldn’t load the data to the map.

       var mymap = L.map('mapid').setView([1.3521, 103.8198], 13);

        L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
            maxZoom: 18,
            id: 'mapbox/streets-v11',
            tileSize: 512,
            zoomOffset: -1,
            accessToken: 'pk.eyJ1IjoiaGlnaHBpZXMiLCJhIjoiY2p0Nml4NmR1MGhxajRhczA1NGFqZXV0NSJ9.c2tn1B3vCBSVni_W9dXQkQ'
        }).addTo(mymap);
        var hospitals = "~/Content/hospitals.geojson";
        var geojsonMarkerOptions = {
            radius: 8,
            fillColor: "#ff7800",
            color: "#000",
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        };
        function onEachFeature(feature, layer) {
            if (feature.properties && feature.properties.Description) {
                layer.bindPopup(feature.properties.Description);
            }
        }
        L.geoJSON(hospitals, {
            onEachFeature: onEachFeature
        }).addTo(map);

This is the error i recieved Invalid GeoJSON object

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can’t load geojson files directly with L.geoJSON, you can use for this Leaflet-Ajax

L.geoJson.ajax(hospitals, {
   onEachFeature: onEachFeature
}).addTo(map);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x