2

(i work on a google maps project) I'm trying to pass latlon variable form function showPosition to hello function, but i cant.

When i print latlon in showPosition function everything work. When i print latlon on hello function i get blank page.

var latlon;

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

function showPosition(position) {
   var latlon = position.coords.latitude + "," + position.coords.longitude;
   /*document.write (latlon); */
}


function hello() {
    variablelatlon = latlon;
    document.write (variablelatlon);    
}

showPosition();
hello();

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}

I change my code to this. But i dont see any coordinates as result.

var x = document.getElementById("demo");
var coordlat;
var coordlng;

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

function showPosition(position) {
   var latlon = position.coords.latitude + "," + position.coords.longitude;
   coordlat = position.coords.latitude;
   coordlng = position.coords.longitude;
}


function hello() {
    document.write (coordlat);    
    document.write (coordlng);    
}


function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}

showPosition();
hello();
Cœur
  • 32,421
  • 21
  • 173
  • 232
Alex Vandyke
  • 73
  • 1
  • 8
  • 1
    Well `showPosition()` never calls `hello()`, and `hello()` isn't set up to receive any incoming parameters. `latlon` is a local variable to `showPosition()`, i.e. is referencable within that scope only. You need to pass it, or write it to something globally accessible. – Mitya Mar 15 '17 at 20:26
  • When you are calling showPosition(), it isn't being passed its 'position' parameter. Also, showPosition() is declaring a LOCAL variable named 'latlon', which means that it won't be setting your global 'latlon' variable. This means that the hello() function will be accessing the global 'latlon' variable which hasn't been touched. – Code4aliving Mar 15 '17 at 20:34
  • Notice that you [must not use `document.write`](http://stackoverflow.com/q/802854/1048572) as well – Bergi Mar 15 '17 at 20:58

2 Answers2

0

Just remove var:

function showPosition(position) {
   latlon = position.coords.latitude + "," + position.coords.longitude;
   /*document.write (latlon); */
}

The fact is, whenever you define a variable by the keyphrase var, you make that variable local for that scope and global for all inside functions.

You already defined latlon as a global variable. When you put var before that inside another function, it would be local inside the function scope. So, it would not affect the global one.

Mojtaba
  • 4,326
  • 4
  • 17
  • 37
  • There's also the problem that when the OP is explicitly calling showPosition() presumably to test it, it isn't being passed a position... – Code4aliving Mar 15 '17 at 20:40
  • I cant remove var. var latlon inside showPosition function is used on getLocation function. – Alex Vandyke Mar 15 '17 at 20:41
  • @AlexVandyke, what I see in the code you put, you can safely remove `var` from the function. If you put the summarized code, please update the question with the full code. – Mojtaba Mar 15 '17 at 20:47
  • Thank you for your help. I change the code and i put the coordinates into another vars. But nothing. :/ – Alex Vandyke Mar 15 '17 at 20:51
  • I update my description with the new code. Can you check it? – Alex Vandyke Mar 15 '17 at 21:06
0

You declared a global variable latlon but then make it local by redeclaring it in function showPosition. This should help:

var latlon;

window.onload = function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
// You declared global variable laton but then make it local by redeclaring it in function showPosition 
function showPosition(position) {
    /*var*/
    latlon = position.coords.latitude + "," + position.coords.longitude;
    /*document.write (latlon); */
}
MERN
  • 2,842
  • 6
  • 13
  • 34
JavaScript
  • 11
  • 2