I'm trying to use getLocation function to get the current location coordinates and use those for distance calculation in future. After I run the following code, I got undefined.
<script type="text/javascript">
var currentLatitude;
var currentLongitude;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getCoordinates);
} else {
document.getElementById("asdfsdafsdaf").innerHTML = "Geolocation is not supported by this browser.";
}
}
function getCoordinates(position) {
currentLatitude = position.coords.latitude;
currentLongitude = position.coords.longitude;
}
var xhr = new XMLHttpRequest();
xhr.open("POST", "rsvpButton.php", true);
var formData = new FormData();
formData.append("userID", 100100);
xhr.send(formData);
xhr.onload=function(){
getLocation();
alert("1"+currentLongitude+" and "+currentLatitude);
//some other codes to display the page
//...
}
</script>
I thought I put those 2 vars in wrong places but after tried several times it still not work. Any help please? Thanks in advance.
Updates: I tried to put the bottom codes into the callback function, but the whole page disappeared.