-1

I am trying to set the longitude and latitude of this function found on W3Schools to a variable (let's just say y and z), then I need to display y:

<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>

For example, if my longitude (y) is 10 and latitude (z) is 15, then it displays

y=10
z=15
JBis
  • 602
  • 1
  • 8
  • 22
  • 3
    you might wanna continue with some more basic tutorials before doing geo location stuff – Isaac Apr 18 '17 at 20:35
  • 1
    *...to set the longitude and latitude of this function* I did not quite understand this sentence. Will you explain please? – jrook Apr 18 '17 at 20:36
  • the function/script/javascript outputs your longitude and latitude. I would like to set that output to a variable. Then display that variable. – JBis Apr 18 '17 at 20:39
  • You probably want to read about ["How do I return the response from an asynchronous call?"](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – trincot Apr 18 '17 at 20:41

1 Answers1

0

You mean like this?

<!DOCTYPE html>
<html>
<body>

<p>Coords will load momentarily.</p>



<p id="demo"></p>

<script>
var x = document.getElementById("demo");

var lat = '';
var lon = '';

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    x.innerHTML = lat + " " + lon;


}

getLocation();
</script>

</body>
</html>

You don't have to declare the values at the global level like I did, but thought it might be useful so you can access it outside of the function.

espradley
  • 2,088
  • 2
  • 16
  • 15
  • Yes! Thank you! I have a couple more question. -What is x.innerHTML? -How would I make it click automatically (like once the page is loaded) – JBis Apr 18 '17 at 20:42
  • x = the "demo"

    x.innerHTML = "", but if you put

    123

    it is 123. If this helps, would appreciate you marking the answer as accepted.
    – espradley Apr 18 '17 at 20:44
  • Also -- updated so that it runs automatically. You'd probably want to use something like jQuery though so you can use $(document).ready(). – espradley Apr 18 '17 at 20:49
  • You can use the script I posted above for now, but if you want to do dom changes after page load, go to www.jquery.com and do their tutorial. – espradley Apr 18 '17 at 20:57
  • Ok. Will do! But is there anychance there is a way to do it in just javascript or no? – JBis Apr 18 '17 at 20:59
  • Yep -- but it's much better in jquery. Here's a good answer on that. http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – espradley Apr 18 '17 at 21:14