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!
2
Answers
You should use
navigator.watchPosition
so you don’t have a popup in the Browser again. Do like:Of course, this design would assign
lat
andlng
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.In order to send the user position within the CF7 email, you need a field with the value of the coordinates returned by your function. Here is an example.