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

333
Visualizações
i get the json data from the external file and i can console log them. but i cannot place the values on a google map

First img is the javascript code. Filling the array and then trying to load them into a map through a for loop https://i.stack.imgur.com/0L0a3.png

this is the console log output of the array... https://i.stack.imgur.com/Ghsw5.png

the markers do not get placed on the specified longitude and latitude given..

error given is: cannot read properties of undefined (reading 'latitude')

var array=[];
$.getJSON('station.json', function (json) {
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            place: item.place,
            latitude: item.latitude,
            longitude: item.longitude,
        });            
    }
}
});


for (var i = 0; i < array.length; i++) {
    marker[i] = new L.marker([array[i][2]['latitude'], array[i][2]]['longitude'])
    .bindPopup(array[i]['place'])
    .addTo(mymap);
}
console.log(array)


  [1]: https://i.stack.imgur.com/d428i.png
  [2]: https://i.stack.imgur.com/Syej2.png
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that $.getJSON is an asynchronous function you need to use that as a promise or handle the map into the callback function

solution 1

var arrayPromise= $.getJSON('station.json').then (json => Object.values(json).map(item => ({
            place: item.place,
            latitude: item.latitude,
            longitude: item.longitude,
        })
)
arrayPromise.then(arr => arr.forEach(({place, latitude, longitude}) => {
   marker[i] = new L.marker(latitude, longitude)
    .bindPopup(place)
    .addTo(mymap);
 
   })
)
       

solution 2


$.getJSON('station.json', function (json) {
  Object.values(json).forEach((item, i) => {
    marker[i] = new L.marker([item.latitude, item.longitude])
     .bindPopup(item.place)
     .addTo(mymap);

  })
})
   
about 4 years ago · Juan Pablo Isaza Relatório

0

Try to use '''Array.foreach''' or '''Array.map''' instead of a for loop, let see the outcome


Array.foreach((item,index)=>{
marker[i]= new L.marker([item.place, item.latitude,item.longitude])});
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