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

176
Views
Grab user geolocation an pass it into contact form 7

I'm trying to set a certain form with WP CF7 and would like to grab the exact user geolocation and pass it into the mail sent to the admin, something like this;

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

only instead of printing lon and lat results -> pass it in mail, perhaps using cookies? (store it and then read it?)

I appreciate any help, Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use navigator.watchPosition so you don't have a popup in the Browser again. Do like:

let lat, lng;
navigator.watchPosition(position=>{
  const pos = position.coords;
  const undef = lat === undefined;
  lat = pos.latitude; lng = pos.longitude;
  if(undef){
    latElement.value = lat; lngElement.value = lng;
  }
}, error=>{
  throw new Error(error.message);
}, {enableHighAccuracy:true});
someLatLngUpdateButton.onclick = ()=>{
  latElement.value = lat; lngElement.value = lng;
}

Of course, this design would assign lat and lng only if they haven't been, then update them when you hit a button. You could constantly update the Elements, but that might be annoying.

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