1

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");
}
Gary
  • 747
  • 3
  • 10
  • 24
  • Does your browser definitely support this? Is `navigator.geolocation` not null? – Rup Aug 15 '10 at 14:02
  • I would have thought so, I have the latest chrome, safari and firefox, saying that I have just tried this in the iphone simulator and sure enough it works, so im totally baffled lol. Thanks for your response – Gary Aug 15 '10 at 14:12
  • Works for me with Opera 10.60 on the below posted jsfiddle page. After denying the permission fail is called and alert displayed – jitter Aug 15 '10 at 14:48

2 Answers2

0

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
  • 9,536
  • 4
  • 41
  • 41
  • Nope definately calling the init function, i put an alert into the getPosition() function which triggers as expected Thanks for your response though. – Gary Aug 15 '10 at 13:56
  • Does the jsFiddle I referenced work for you? Which browser are you using? – MvanGeest Aug 15 '10 at 13:58
  • Here's someone else's compatibility list http://stackoverflow.com/questions/1349064/which-devices-support-javascript-geolocation-via-navigator-geolocation/3180478#3180478 – Rup Aug 15 '10 at 14:04
  • 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 Aug 15 '10 at 14:04
  • I don't have that hardware (nor software, for that matter). Can someone else confirm this behaviour? – MvanGeest Aug 15 '10 at 14:06
0

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
  • 29,976
  • 9
  • 71
  • 89
  • 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 again Gary – Gary Aug 21 '10 at 10:58
  • did you try adding the timeout and seeing if the fail function was called? – Galen Aug 21 '10 at 18:42