开发者

Geo-location Help

Hi can anybody tell me what exactly i am doing wrong I cant see开发者_如何学编程m 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");
}


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)).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜