skip to Main Content

I am a little stuck with this error and was hoping someone could shed some light on the issue.

At the moment I am trying to add a geoJSON layer to my Leaflet Map, more specifically a multi polygon which shows the country borders. I am using an AJAX request to a PHP routine that returns the data from a large geo.json file containing country data.

Where I am stuck is trying to add the country borders/polygon to the map, the following error keeps printing to the console.

CONSOLE LOG ERROR:

Layer.js:52 Uncaught TypeError: t.addLayer is not a function
    at i.addTo (Layer.js:52)
    at Object.success (index.js:104)
    at fire (jquery-3.6.0.js:3500)
    at Object.fireWith [as resolveWith] (jquery-3.6.0.js:3630)
    at done (jquery-3.6.0.js:9796)
    at XMLHttpRequest.<anonymous> (jquery-3.6.0.js:10057)

I suspect that I am doing something wrong in my ajax success function with how I am trying to add the data to the map however after a couple of days of trying different things I now need advice from the experts :).

Also I have checked that my files are linked correctly.

Any suggestions would be most appreciated, thank you!

SOME OF MY CODE:

//LEAFLET MAP INITIALISATION

var map = L.map('map').setView([51.509, -0.08], 15);

L.tileLayer('https://tile.thunderforest.com/atlas/{z}/{x}/{y}.png?apikey={accessToken}', {
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="http://www.thunderforest.com/">Thunderforest</a>',
    maxZoom: 18,
    id: 'thunderforest/atlas',
    accessToken: 'a794617134d14b1f82f1cd09d35bca51'
}).addTo(map);

    var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]).addTo(map);

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

});

//COUNTRY BORDER AJAX REQUEST


$('#borderSearch').click(function(){
        
        $.ajax({
        url: "lib/php/border.php",
        type: "POST",
        dataType: 'json',
        data: {
            code: $('#countryName').val()
        },

        success: function(result) {

        var borderData = JSON.stringify(result);
        
        var parsedGeoJson = JSON.parse(borderData);

        L.geoJSON(parsedGeoJson).addTo(map);

    },

        error: function(jqXHR, textStatus, errorThrown){
            console.log("Request Failed");
        }
    });

  });

2

Answers


  1. Your map variable is initialized in another function and not accessible global.

    var map;
    $.ready(function(){ // Your function, I don't know what you do here, but I that you call this code in a function. Maybe `.ready(` or something like this
        map = L.map('map').setView([51.509, -0.08], 15);
    
        L.tileLayer('https://tile.thunderforest.com/atlas/{z}/{x}/{y}.png?apikey={accessToken}', {
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="http://www.thunderforest.com/">Thunderforest</a>',
            maxZoom: 18,
            id: 'thunderforest/atlas',
            accessToken: 'a794617134d14b1f82f1cd09d35bca51'
        }).addTo(map);
    
        var polygon = L.polygon([
            [51.509, -0.08],
            [51.503, -0.06],
            [51.51, -0.047]
        ]).addTo(map);
    
        var marker = new L.Marker([51.509, -0.08]);
        marker.addTo(map);
    });
    

    And know that map variable is global and you can use it in the other functions like the click listener.

    I’m pretty sure that if you add console.log(map) before L.geoJSON(parsedGeoJson).addTo(map); that it will print undefined

    Login or Signup to reply.
  2. Sometimes Map is not accessible globally. To overcome the issue, initialize the map outside the function.

    var map;
    $.ready(function(){ // Your function, I don't know what you do here, but I that you call this code in a function. Maybe `.ready(` or something like this
        map = L.map('map').setView([72.456135809000045,23.039693043000057], 13);
    
        L.tileLayer('https://tile.thunderforest.com/atlas/{z}/{x}/{y}.png?apikey={accessToken}', {
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="http://www.thunderforest.com/">Thunderforest</a>',
            maxZoom: 18,
            id: 'thunderforest/atlas',
            accessToken: 'a794617134d14b1f82f1cd09d35bca51'
        }).addTo(map);
    
        var polygon = L.polygon([
            [72.456135809000045,23.039693043000057], 
            [72.455593154000042,23.039557067000032], 
            [72.455180161000044,23.03942915500005], 
            [72.455047606000051,23.039388099000064]
        ]).addTo(map);
    
        var marker = new L.Marker([72.456135809000045,23.039693043000057]);
        marker.addTo(map);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search