0

I'm doing next:

First thing - I'm checking if I already have loaded latitude and longitude data. If I do have, I'm saving that into array named "location".

Second thing - if I don't have any loaded data, I'm trying to get current position. After that, I'm trying to save latitude and longitude for current position. And, at the end, I'm calling setMap(); function where I check location array and where I'm generating a map.

Problem:

Well, as I said it...inside "getCurrentPosition", I'm trying to set current position into "location" array, and after that I'm trying to take those values inside setMap(); function. Here's the problem, but only when i set "location" values inside "getCurrentPosition". If I set "location" values manually before (from "data" array), everything works fine. Also, when I call a setMap() function inside "getCurrentPosition", everything works fine...but when I call it from outside, won't work. Can someone explain me what's going on and what can I do?

Here's my code:

location = new Array();
if (data.lat) {
        location['lat'] = data.lat;
        location['lng'] = data.lng;
    } else {
        if (!navigator.geolocation) {
            //
        } else {        
            navigator.geolocation.getCurrentPosition(function(position) {
                location['lat'] = position.coords.latitude;
                location['lng'] = position.coords.longitude;
            });
        }       
    }
setMap();

Thank you.

dperitch
  • 1,869
  • 2
  • 17
  • 22
  • Ok, I've found something interesting here: http://stackoverflow.com/questions/7189492/javascript-assigning-the-return-value-of-a-callback-function-to-global-variable – dperitch Sep 02 '11 at 08:33

1 Answers1

0

You are setting a different variable inside of getCurrentPosition. I would suggest researching variable scope. Check out this question.

--edit--

I did not realize that the function you were using was a part of Javascript, I thought you had written/gotten it from somewhere.

Also, I stand corrected about the scope. Since you are using an array, the scope is fine, you should be able to set it inside the callback function just fine.

I ran your code and got some strange printouts when I did a console.log on location. I took a guess, and it appears that location is a reserved word. I bet that if you change the name of location to something else, your code should work fine.

--edit--

I should have realized this sooner actually, since location is the same as window.location, which is the browser bar location!

Community
  • 1
  • 1
AsherMaximum
  • 942
  • 2
  • 11
  • 17
  • thanks for your answer...i'm researching that question but still don't have solution. – dperitch Sep 01 '11 at 17:49
  • I'm assuming your `getCurrentPostion` function returns the current position in an object, correct? – AsherMaximum Sep 01 '11 at 18:04
  • what your code is doing is passing an anonymous function _to_ `getCurrentPostion`, instead of taking a value _from_ it. – AsherMaximum Sep 01 '11 at 18:05
  • but `getCurrentPosition` takes only three arguments, and those are `successCallback`, `errorCallback` and `options`. And you can't just assign `getCurrentPosition` to some kind of variable. – dperitch Sep 01 '11 at 18:32
  • Heh, must disappoint you, but that's also not a problem. In the example above, I've wrote down `location` variable just to present all clearly. In real example, I'm using var `homeloc` which, as I already said, works fine if I settle data outside `getCurrentPosition` method. Thanks anyway...hope I'll find solution to this problem. – dperitch Sep 02 '11 at 07:47
  • I've found something interesting here http://stackoverflow.com/questions/7189492/javascript-assigning-the-return-value-of-a-callback-function-to-global-variable – dperitch Sep 02 '11 at 08:34
  • ok, that makes sense, that `getCurrentPostition` is asynchronous. I would suggest just moving your call to `setmap` to both places where `location` is set. – AsherMaximum Sep 06 '11 at 19:13