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

239
Visualizações
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 à 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