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

238
Vistas
JQuery document.ready function returning undefined from window.navigator.geolocation.getCurrentPosition()

As part of a portfolio project for a web dev course, I'm trying to grab the device lat and long coords for a leafletJS based map app for use in later API calls but I'm having a synchronicity issue with JQuery's document.ready function. In my script.js file I have:

1 const getCoordsFromDeviceLocation = () => {
2     window.navigator.geolocation.getCurrentPosition((pos) => {
3         ({latitude, longitude} = pos.coords);
4         console.log(latitude);
5         console.log(longitude);
6         return {latitude, longitude};
7     })
8 }

And then:

9  $(function () {
10     const coords = getCoordsFromDeviceLocation();
11     console.log(coords);
12 })

The log is returning, in order:

11 - undefined

4 - (correct lat coord)

5 - (correct long coord)

I know it has something to do with synchronicity with line 11 returning first but I'm unsure about how to grab the coords within the doc.ready function. Can anyone point me in the right direction?

about 4 years ago · Juan Pablo Isaza
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