Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

how to display geojson data on a leaflet map javascript

I am trying to display the coordinates I have saved in JSON format on a leaflet map. Neither the marker of the coordinates is showing nor am I getting any error. here is my geojson data and the code that should display the records on the map. Where might I be doing it wrong? am using leaflet 1.9.4 cdn css and js links

here is the code in the script

// map scripts

    var defaultLocation = [-1.286389,36.817223]

    var map = L.map('map')
        .setView(defaultLocation, 13);
        //.center(defaultLocation);
    
    var tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 19,
        attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    }).addTo(map);

    var marker = new L.Marker([-1.2787474907651892,36.821225881576545]);
    marker.addTo(map);

var geoJsondata = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {
                    "name": "Example 1",
                    "amenity": "Baseball Stadium",
                    "iconSize": [50, 50],
                    "locationId": 1,
                    "image": "https://images.unsplash.com/photo-1688976694262-89230d6133ba?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw3fHx8ZW58MHx8fHx8&auto=format&fit=crop&w=500&q=60",
                    "popupContent": "This is where the Rockies play!"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-1.2822121651282061, 36.82157993316651]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "name": "Example 2",
                    "amenity": "Baseball Stadium",
                    "iconSize": [50, 50],
                    "locationId": 2,
                    "image": "https://images.unsplash.com/photo-1688728981543-df24d24d0116?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyNXx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=500&q=60",
                    "popupContent": "This is where the Rockies play!"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-1.2842580516508735, 36.816703677177436]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "name": "Example 3",
                    "amenity": "Baseball Stadium",
                    "iconSize": [50, 50],
                    "locationId": 3,
                    "image": "https://images.unsplash.com/photo-1688966838599-05e6cf6628cf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwzMnx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=500&q=60",
                    "popupContent": "This is where the Rockies play!"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-1.2815256914612605, 36.81707382202149]
                }
            }
            
        ]
    }

    L.geoJSON(geoJsondata).addTo(map);
0 likes
3 replies
iftekhs's avatar

Can you provide the full code like what is map = ? and the html and also how are you loading leaflet js using CDN ?

1 like
iftekhs's avatar

@stephen You can follow these steps ->

Check for the existence of the map container: Make sure you have an HTML element with the id attribute set to "map" in your HTML code. The Leaflet map will be rendered within this element. For example, you should have a element in your HTML structure.

Verify the order of script inclusion: Ensure that you are including the Leaflet JavaScript library before your custom Leaflet code. The Leaflet library must be loaded and available before you try to use any Leaflet functions or objects.

Check for JavaScript errors in the developer tools: Open the developer tools in your browser, navigate to the "Console" tab, and look for any error messages related to Leaflet or your custom code. This can help identify any issues or errors that may be preventing the code from running correctly.

Please or to participate in this conversation.