2

How can I save lat and lng values to variables using HTML5 geolocation API?

This is my code, copied from w3schools. How can I save coordinates to variables like var x= position.coords.latitude; and var y= position.coords.longitude; instead of just showing the values like the code is doing right now? I am beginner with javascript so I don't know how to do this

 <!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>

var x=document.getElementById("demo");
window.onload = function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.watchPosition(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>

And in my application I need to send values of those variables to server every minute. Would it be better to do the geolocation with watchposition or execute function that does getcurrentposition every minute?

1 Answer 1

1

Adding them to variables is as easy as:

var lat = position.coords.latitude;
var lon = position.coords.longitude;

OR as an object:

var coords = {lat: "", lon: ""};

Then to use the object in your code:

function showPosition(position)
{
   coords.lat = position.coords.latitude;
   coords.lon = position.coords.longitude;

   x.innerHTML="Latitude: " + coords.lat + 
   "<br />Longitude: " + coords.lon;  
}

As for sending the variables to a server this depends on what your serverside technology is but you will find many examples on google.

function sendToServer(){
   // here you can reuse the object to send to a server
   console.log("lat: " + coords.lat);
   console.log("lon: " + coords.lon);
}
Sign up to request clarification or add additional context in comments.

5 Comments

If i do var lat = position.coords.latitude; var lon = position.coords.longitude; alert(lat); alert only shows "undefined"
Sorry, yes of course i was not setting the objects properties in the fucntion where position is passed in. My example is only useful if you want to use the coords object in another function, like your function to send to a server, you understand it? I have editted it.
So how can I pass values to variables that would work even when i alert the variable?
Look at my edit, in the showPosition function, I am setting the coords objects properties to the values from the position object. They are now stored in the coords object, you can alert them anywhere you like or do whatever you want with them.
It seems that I'm facing the same issue here , which i can't pass the lat and long to some variable so I can use it with my code, stackoverflow.com/questions/11475192/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.