Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!