Im a javascript learner and have some trouble with my script i hope someone has time to help me. In short I want the script to get position of user, save it to a variable and send the position of an object to the server.
I have 3 different objects in my actual script. I want to assign a position to the object before sent to server. I know this can be solved more elegant than having 3 functions (getPositionobject1, 2, 3) that all contain the same if statement. But this is a good starting point.
This script run OK, but sendToServer get called before geometry/position has been assign. Any advice of how i can arrange my code to make sure sendToServer() is called after geometry is assigned?
Here is a fiddle https://jsfiddle.net/mw7cgwwz/22/
function getPositionobject1() {
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
sendObjectOneToServer(); //This get called before geometry is defined
}
function displayPosition(position) {
alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude)
geometry = (position.coords.latitude + "," + position.coords.longitude).toString()
}
sendToServer() {
var postData =
'stuff' + geometry
}