Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
Leaflet JS Map Markers Not Changing

I am trying to add map markers to my leaflet map, using coordinates returned from a call to an api. I origionally set the map marker to a specific location (leaflet wasnt happy with a blank array), I then update the marker variable when a user selects a country from a drop down list. I can see that the marker variable is being updated after each user selection, but the marker on the map doesnt change location? heres the parts of the JS code involved:

let baseMaps = {
    
    "Hybrid": hybrid,
    "Terrain": terrain,
    "Streets": streets,
    "Sat": sat,
    "Pulchritudinous": pulchritudinous,
    
};
//variable to hold coordinates of marker
let markerTest = L.marker([54, -2]);

let overlayMaps = {
    "Marker" : markerTest
};

let map = L.map('map', {
    layers: [pulchritudinous, streets, sat, terrain, hybrid, markerTest]
});

//add baselayers to map
L.control.layers(baseMaps, overlayMaps).addTo(map);

here is the api call and return code;

function markers(){

    $.ajax({
        url: "libs/php/markerCapital.php",
        type: 'GET',
        dataType: 'json',
        data: {
            country: country,
        },
         success: function(result) {

            // console.log(JSON.stringify(result, null, 4));
        
            if (result.status.name == "ok") {
            
                console.log("success markerCaptial");
        
                //reset cities array
                cities = [];

                //populate cities array with data
                for(let i = 0; i < result.data.geonames.length; i++){
                    cities.push([result.data.geonames[i].asciiName, result.data.geonames[i].lat, result.data.geonames[i].lng, result.data.geonames[i].population]);
                }

                //Add to map
                markerTest = L.marker([cities[0][1], cities[0][2]]);
                console.log("MARKER TEST: ", markerTest);
            }
        }

markers() gets called everytime a user changes location.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Found the solution. In the api call response, update markerTest as follows:

markerTest.setLatLng([cities[0][1], cities[0][2]]).update();
                console.log("MARKER TEST: ", markerTest);

Not sure its the best way to do it, but it works.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda