views:

93

answers:

2
+1  Q: 

Geo-location Help

Hi can anybody tell me what exactly i am doing wrong I cant seem to get either the success or fail function to run.

function init(){;
    // Get the current location
    getPosition();      
}

function getPosition(){
    navigator.geolocation.getCurrentPosition(success, fail);    
}   

function success(position) 
{
    alert("Your latitude: " + position.coords.latitude + "longitude: "
        + position.coords.longitude);
}

function fail()
{
    alert("Your position cannot be found");
}
A: 

Did you forget to call the init function somewhere? The code runs fine in the jsFiddle found on http://jsfiddle.net/mhj82/ (using Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729)).

MvanGeest
Nope definately calling the init function, i put an alert into the getPosition() function which triggers as expectedThanks for your response though.
Gary
Does the jsFiddle I referenced work for you? Which browser are you using?
MvanGeest
Here's someone else's compatibility list http://stackoverflow.com/questions/1349064/which-devices-support-javascript-geolocation-via-navigator-geolocation/3180478#3180478
Rup
I must admit i have clicked the link, in safari it asks me if I will allow location services, but nothing after that and chrome does nothing. :-S something odd going on here.Macbook, Safari Version 5.0 (6533.16), Chrome 5.0.375.126(latest version i believe)
Gary
I don't have that hardware (nor software, for that matter). Can someone else confirm this behaviour?
MvanGeest
A: 

I'm also having issues with geolocation. It depends on what browser you're using. If you try it on google chrome i'm sure it will work, it works flawlessly for me with chrome.

use this instead to test:

navigator.geolocation.getCurrentPosition(success, fail, {timeout: 5000});

It will try for 5000milliseconds before executing the fail function. It seems that firefox just keeps trying forever without ever being able to get the location. Setting the timeout forces it to quit. Safari works rarely. I'm currently investigating this as well...good luck.

EDIT After restarting my computer geolocation seems to work fine. weird. try it with the timeout and let me knwo what happens. it may be getting "stuck" like mine was.

Galen
Thanks for your response Galen,Unfortunately I'm still facing the same issue, don't understand why! even after a restart.It does seem to be getting stuck like you suggest I'm just wondering if there is a global setting on the mac to prevent geo-location.That would explain why it's not working in any browser.Thanks againGary
Gary
did you try adding the timeout and seeing if the fail function was called?
Galen