0

I have 2 js files, 1 is to store the location data in an object and the other is the event handler for the click events and such.

This works:

var Geology = {
    coords: {},
    error: '',
    setPosition: function (position) {
        Geology.coords = position.coords;
        // DEBUG
        for (var prop in Geology.coords)
            console.log(prop + ': ' + Geology.coords[prop]);
    },
    setError: function (error) {
        Geology.error = error;
    }
};

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(Geology.setPosition,Geology.setError,{timeout:10000});
}

But i want to be able to update the location data if necessary by a click or timer. Any time i try to do that it doesn't assign the vars to Geology like it does initially.

like this:

jQuery(document).ready(function($){
    $('.toggle').click(function(){
        //- get geo location if available
        if (typeof Geology === 'object') {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(Geology.setPosition,Geology.setError,{timeout:10000});
            }
        }
    });
});

I don't get errors in the console but it just fails and does not run the callback. However, it does run the getCurrentPosition method because I've tested to see if it makes it through the conditionals.

Emery King
  • 3,550
  • 23
  • 34
  • _"I've tested to see if it makes it through the conditionals"_ so you're 100% sure that `typeof Geology === 'object'` returns true, correct ? – wakooka Jan 15 '13 at 04:07
  • yes, i can make alerts happen inside of that. – Emery King Jan 15 '13 at 04:08
  • You can just call `console.log(Geology.coords)`. You shouldn't need to loop through the properties. Both work, but it might be easier this way. – Tom Pietrosanti Jan 15 '13 at 04:08
  • I just ran this in Firefox 18 on Mac with no problems. There will be a delay when geolocation is running before the callback fires - make sure you're waiting long enough. – Tom Pietrosanti Jan 15 '13 at 04:13
  • i'm on chrome, the console never gets updated after the click if i remove the initial hydrator. – Emery King Jan 15 '13 at 04:14

1 Answers1

1

I think you need to set the maximum_age parameter, otherwise the results will be cached forever :

navigator.geolocation.getCurrentPosition( 
      geo_success, 
      geo_error, 
      { maximumAge:60000 } // set to 1min (in ms)
);

You also have two other parameters : enableHighAccuracy and timeout

More info: https://developer.mozilla.org/en-US/docs/Using_geolocation

EDIT : I just found this, it's probably related to your issue navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

Community
  • 1
  • 1
wakooka
  • 1,398
  • 10
  • 15