地理位置帮助
嗨有人可以告诉我我到底做错了什么吗 我似乎无法运行成功或失败函数。
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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否忘记在某处调用
init
函数?代码在 http://jsfiddle.net/mhj82/ 上找到的 jsFiddle 中运行良好(使用 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))。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)).我也遇到了地理位置问题。这取决于您使用的浏览器。如果你在 google chrome 上尝试一下,我相信它会起作用,它对我来说在 chrome 上完美地工作。
使用它来测试:
它将在执行失败函数之前尝试 5000 毫秒。火狐似乎一直在尝试,但始终无法获取该位置。设置超时会强制其退出。 Safari 很少工作。我目前也在调查这个问题......祝你好运。
编辑
重新启动计算机后,地理定位似乎工作正常。诡异的。尝试超时,让我知道会发生什么。它可能会像我一样被“卡住”。
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:
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.