本文介绍了navigator.geolocation.getCurrentPosition在chrome和firefox中总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试测试我的
navigator.geolocation.getCurrentPosition网页时,出现奇怪的行为。我的
测试结果和代码如下: c $ c> function detectLocation()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(geocodePosition,onError,{timeout:30000});
navigator.geolocation.watchPosition(watchGeocodePosition);
}
else
{
onError();
$ b $ p
$ b 当bodyonload事件是调用。我曾尝试将超时更改为10000和20000,但我仍然得到相同的结果。我还允许crome和firefox来获取我的位置。
结果:
- 使用chrome(v 17.0.963.79 m),当navigator.geolocation.getCurrentPosition被调用时,结果总是进入onError
函数。
- 使用Firefox(v 10.0.2)当navigator.geolocation.getCurrentPosition被调用时,总是去onError函数
。
- 使用IE(v 9),结果非常好,我得到了我的当前位置。
任何人都可以帮助我在这种奇怪的情况?我真的没有任何想法来解决这个问题,而且我在我的项目最后期限上很忙。谢谢之前。
编辑:
这几天我得到了一些进展,错误代码是2, https://maps.googleapis.com/maps/api/browserlocation/json?browser=chromium&sensor=true中的网络位置提供商:响应格式错误。还有没有解决,有没有人知道如何解决这个问题?解决方案
当HTML页面托管在Web服务器上而不是从文件系统打开时。为了测试,我直接从我的C:驱动器打开文件,它的回调不起作用,然后托管在Internet信息服务(IIS)上的文件和回调没有工作。
< html>
< body onload =detectLocation()>
< div id =status>< / div>
< script type =text / javascript>
function detectLocation()
{
log(detectLocation()starting);
if(navigator.geolocation)
{
log(navigator.geolocation is supported);
navigator.geolocation.getCurrentPosition(geocodePosition,onError,{timeout:30000});
navigator.geolocation.watchPosition(watchGeocodePosition);
}
else
{
log(navigator.geolocation not supported);
$ b函数geocodePosition(){
log(geocodePosition()starting);
function watchGeocodePosition(){
log(watchGeocodePosition()starting);
函数onError(错误){
log(error+ error.code);
}
函数日志(msg){
document.getElementById(status)。innerHTML = new Date()+::+ msg +< br /> + document.getElementById(status)。innerHTML;
}
< / script>
< / body>
< / html>
I got strange behavior when I tried to test my"navigator.geolocation.getCurrentPosition" web page. Here is mytesting result and code:
my code:
function detectLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
navigator.geolocation.watchPosition(watchGeocodePosition);
}
else
{
onError();
}
}
this function was run when "body" onload event was called. I had tried to change the timeout to 10000 and 20000, but I still got same result. I also allowed crome and firefox to get my location.
result:
- Using chrome (v 17.0.963.79 m), result always went to onErrorfunction when navigator.geolocation.getCurrentPosition was called.
- Using Firefox (v 10.0.2), result always went to onError functionwhen navigator.geolocation.getCurrentPosition was called.
- Using IE (v 9), result was fantastic, I got my current location.
can anyone help me in this strange situation? I really didn't have any idea to solve this problem and I was in hurry on my project deadline. Thanks before.
EDIT :For this couple days I got some progress, the error code code is 2 with a message "Network location provider at 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=chromium&sensor=true' : Response was malformed". Still unsolved, does anyone know how to solve this?
解决方案
I simulated this problem and found that the success callback functions were only called when the html page was hosted on a web server and not when opened from a filesystem.
To test I opened the file directly from my C: drive and it the callbacks didn't work and then hosted the file on Internet Information Services (IIS) and the callbacks did work.
<html>
<body onload="detectLocation()">
<!-- This html must be hosted on a server for navigator.geolocation callbacks to work -->
<div id="status"></div>
<script type="text/javascript">
function detectLocation()
{
log("detectLocation() starting");
if (navigator.geolocation)
{
log("navigator.geolocation is supported");
navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
navigator.geolocation.watchPosition(watchGeocodePosition);
}
else
{
log("navigator.geolocation not supported");
}
}
function geocodePosition(){
log("geocodePosition() starting");
}
function watchGeocodePosition(){
log("watchGeocodePosition() starting");
}
function onError(error){
log("error " + error.code);
}
function log(msg){
document.getElementById("status").innerHTML = new Date() + " :: " + msg + "<br/>" + document.getElementById("status").innerHTML;
}
</script>
</body>
</html>
这篇关于navigator.geolocation.getCurrentPosition在chrome和firefox中总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!