Complementing the response of Pedro Augusto, but using the code snippet you posted, the easiest would be to do the post in function showPosition(), although in this case maybe she should change her name to treatPosition() or something like that:
var x = document.getElementById("demo");
function getLocation() {
if (! navigator.geolocation) {
x.innerHTML = "Geolocation is not supported by this browser.";
} else {
navigator.geolocation.getCurrentPosition(function (pos) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
$.ajax({
url: "path/to/service.php",
type: "POST",
data: {
lat: position.coords.latitude,
long: position.coords.longitude,
text: x.innerHTML
}
});
});
}
}
Of course, the truth code depends on the characteristics of the service: what it expects with respect to method, parameter names, which URL, which MIME type of the request, etc.
I am correct in assuming that by "this function", you are referring to the content of the element
x? That is, the string "Latitude: XXX<br>Longitude: OOO"?– Wtrmute
Yes sir, I just want to return to latitude and longitude.
– user70266