简体   繁体   English

地理位置帮助

[英]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");
}

Did you forget to call the init function somewhere? 您是否忘了在某个地方调用init函数? 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)). 该代码在http://jsfiddle.net/mhj82/ (使用Mozilla / 5.0(Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.8))上找到的jsFiddle中运行良好。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. 如果您在google chrome上尝试使用它,我相信它会起作用,但对于chrome来说,它对我来说完美无瑕。

use this instead to test: 用它代替测试:

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

It will try for 5000milliseconds before executing the fail function. 在执行失败功能之前,它将尝试5000毫秒。 It seems that firefox just keeps trying forever without ever being able to get the location. 似乎firefox一直在不断尝试,始终无法获得位置。 Setting the timeout forces it to quit. 设置超时会强制退出。 Safari works rarely. Safari很少工作。 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. 它可能像我的一样被“卡住”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM