0

I need to locate my self in my web app. I am using google maps to pin my location. The problem i have is with function that returns my current location:

function setLocation() {
            var temp = { lat: 0, lng: 0 };
            navigator.geolocation.getCurrentPosition(function (position, temp) {
                //here is all ok, temp.lat and tem.lng are assigned correctly
                temp.lat = position.coords.latitude;
                temp.lng = position.coords.longitude;
            });
            //here temp.lat and temp.lng are again 0    
            return temp;
        }

I put some comments in my code which describe the problem. What am I doing wrong? Thanks for reading this. p.s. I am new at java script.

Petar Mijović
  • 337
  • 3
  • 15
  • @MikeC If you see the connection, can you please give me a code snippet that will fix my code? – Petar Mijović Nov 11 '16 at 17:45
  • 2
    No, I'm not going to write your code for you. That's not what Stack Overflow is for. Read the linked question and learn from it. – Mike Cluck Nov 11 '16 at 17:47
  • 2
    I agree, the linked question answers your question if you spend the time to understand it. –  Nov 11 '16 at 17:49

1 Answers1

0

The Problem is You Are Using an Asynchronous Function

The return value is not correct because getCurrentPosition() has not executed yet. You must use a callback function that will be passed the result after getCurrentPosition() is finished. As Mike C said, a more detailed description of asyncronous function can be found at How do I return the response from an asynchronous call?

function setLocation(callback)
{
    var temp = {
        lat: 0,
        lng: 0
    };

    navigator.geolocation.getCurrentPosition(function (position, temp)
    {
        temp.lat = position.coords.latitude;
        temp.lng = position.coords.longitude;
        if ("function" === typeof callback)
        {
            callback(temp);
        }
    });
    return;
}


setLocation(function (coords)
{
    // This will alert the correct data
    alert(coords.lat + " " + coords.lng);
});

Promises Can Be a Better Solution

Call backs can get out of hand quickly so I would recommend using Promise object. Tutorial at https://davidwalsh.name/promises. The main idea is that you can have an easy to read/understand code flow while using asynchronous tasks. An example for your code would be.

function setLocation()
{
    return new Promise(function (resolve)
    {
        var temp = {
            lat: 0,
            lng: 0
        };

        navigator.geolocation.getCurrentPosition(function (position, temp)
        {
            temp.lat = position.coords.latitude;
            temp.lng = position.coords.longitude;
            resolve(temp);
        });
        return;
    });
}

setLocation
    .then(function (coords)
    {
        // This will alert the correct data
        alert(coords.lat + " " + coords.lng);
    });
Community
  • 1
  • 1
L Bahr
  • 2,723
  • 3
  • 22
  • 25
  • 1
    Don't just hand out code. It doesn't teach the asker anything and sets a bad precedent. – Mike Cluck Nov 11 '16 at 17:50
  • Did you not see the comments on the question? Also, a dump of code with no clarifying commentary is hardly a good answer. See [answer] for tips on answering questions. – Heretic Monkey Nov 11 '16 at 17:51
  • 1
    I usually add the code fix first. After I explain why it works. That way the person can get past the problem quickly and then learn what the issue was. Some times just seeing how to do it is enough for people to understand what was wrong. – L Bahr Nov 11 '16 at 17:54
  • I've reversed my downvote based on the expansion of the answer. –  Nov 11 '16 at 19:02