0

I have 2 global variables and changing them into a function but it's still shows as undefined.

var lat
var long;
function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else
    {
        console.log("Geolocation is not supported by this browser.");
    }
}
function showPosition(position)
{
    lat = position.coords.latitude;
    long = position.coords.longitude;
}
getLocation();
console.log(lat + "   " + long);

I have checked other questions on this site but none seem to have answer to this. When I check inside the showPostion() function, it shows actual coordinates. I am new to js please help.

Edit:

I have another function in which I want to access global variables.

function generate_list(params, fromPageLoad)
{
    var ajax_url = jQuery('#ajax_post_url').val();
    var data = { action: 'generate_list', latitude: lat, longitude: long };
    var extended_data = jQuery.extend(data, params);

    jQuery.ajax({
    type: 'POST',
    data: extended_data,
    url: ajax_url,
    success: function (data) {
            //console.log(data);}
}

This function is getting called on the loading of the page with dynamic parameters.

I also tried to return lat lang in showPosition() but it's not working. The 'POST' or 'GET' method doesn't return coordinates variable.

function showPosition(position)
    {
        var ajax_url = jQuery('#ajax_post_url').val();
        var lat = position.coords.latitude;
        var long = position.coords.longitude;
        jQuery.ajax({
            type: 'POST',
            data: { lat: lat,long: long },
            url: ajax_url,
            success: function(data)
            {
            }
        });

        console.log("Latitude: " + position.coords.latitude +
            "Longitude: " + position.coords.longitude);
    }

If user allow the location, I want to change the list.

the_badev
  • 47
  • 6

2 Answers2

1

geolocation.getCurrentPosition is async API. Try moving console.log inside function showPosition(position).
A lot (if not all) of APIs with callback are async. Be careful out there!

Nikko Khresna
  • 1,024
  • 8
  • 10
  • Inside showPosition() is working, but I want to pass coordinates to another function so I assigned it to global variable. – the_badev Mar 27 '20 at 19:58
  • 1
    just call the function inside showPosition()? – Nikko Khresna Mar 27 '20 at 20:01
  • because actually the global variables are changed, so if that function is run at later time, it should be able to get the lat and long. If it runs immediately, it needs to be in showPosition() – Nikko Khresna Mar 27 '20 at 20:04
  • Unfortunately, I can not add another function into showPosition() as it loads on loading of the page. – the_badev Mar 27 '20 at 20:09
  • @the If you can assign to global variables from it, why can’t you call other code from within it? You will have to do that one way or another. – deceze Mar 27 '20 at 20:30
  • The other function has parameters and that parameters are set dynamically from another file. – the_badev Mar 27 '20 at 20:34
  • @deceze it's common for the consumer to know about the resource (in this case, a function in another file knows about the global variables), and sometimes it doesn't make sense to refactor the source code of the resource to call all the consumers. In this case, an easier refactor would be to store the result as a global promise instead of mutable globals `lat` and `long`. – Patrick Roberts Mar 27 '20 at 20:34
  • @the_badev if you can edit your question to highlight why it's inconvenient to call your other function from within `showPosition()`, I'll vote to re-open your question and explain my comment above with actual code. – Patrick Roberts Mar 27 '20 at 20:38
  • I edited my question with explanation. – the_badev Mar 27 '20 at 20:43
0

Answer you dear friend

let lat;
let long;
navigator.geolocation.getCurrentPosition(success, null, {});

function success(pos) {
  lat = pos.coords.latitude
  long = pos.coords.longitude

  console.log('lat =',lat)
  console.log('long =',long)

}