Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda