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

274
Views
Google map API navigator.geolocation.getCurrentPosition(showPosition). How to get latlng values out of function

I am new to Stackoverflow, JavaScript and Google Map API. The issue is I am currently getting user current location with below code but how can I return the Latlng value from this and use it in other places in script like in making call for directionsService .

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
    //console.log("location--Yes");
   } else {
    alert("Geolocation is not supported by this browser.");
    }
   }

function showPosition(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
  var latlng = new google.maps.LatLng(lat, lng);
  const marker = new google.maps.Marker({
    position: latlng,
    visible: true,
    map: map
  });
  marker.setMap(map);
    //console.log("marker should display");
    //return latlng;
    }

Thanks for help!

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!