HTML5 GeoLocation API - 不需要回叫 [英] HTML5 GeoLocation API - no callback is called whatsoever

查看:118
本文介绍了HTML5 GeoLocation API - 不需要回叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检查GeoLocation API的代码片段。如果可用,我试图获得当前位置。但是,根据浏览器的不同,代码的工作方式也不同:在IE中,每个F5 refres都会调用successCallback
$ b


  • 在FF中,我在Chrome中获取了successCallback或errorCallback,错误代码= 2(POSITION_UNAVAILABLE)

  • 我得到successCallback或...没有任何结果。我等着,等一下,没有任何东西



其实我需要以一致的方式调用successCallback ...是否有改变来实现它?

代码片段:

  function successCallback(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
alert(您的位置是:+ latitude +,+ longitude);
}

函数errorCallback(错误){
console.log(error);
}

if(Modernizr.geolocation){
navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{maximumAge:0});
alert(启用地理位置);
} else {
alert(geolocation is not enabled);
}

快速注释1:

  alert(geolocation is enabled); 

每次都会被调用



快速记录2:

设置:

  maximumAge:1 

效果更好。现在Chrome& IE每次调用successCallback。只有FF吓到我了,它随机调用successCallback。 解决方案

你是浏览器开发人员的心血来潮,他们是如何尝试根据WiFi接入点指纹识别或IP查找找出你的位置的。



出于好奇,我测试了代码( http: //jsfiddle.net/YbtSZ/2/ ),并发现它的工作不一致。



Mac 10.7.2


  • Mac上的Chrome浏览器 - 着作

  • b $ b
  • Mac上的Safari - 无法正常工作



XP原生




  • XP上的FF 9 - 作品

  • XP上的IE 8不起作用(不支持地理位置)



在Mac上运行虚拟化的XP


  • XP上的Chrome(在Mac中虚拟化) - 无效(POSITION UNAVAILABLE)

  • XP上的FF 9(在Mac中虚拟化) - 无效(POSITION_UNAVAILABLE)

  • XP上的IE 8(在Mac中虚拟化) - 不起作用(不支持地理定位)


I have a code snippet that checks for GeoLocation API. If it's available, I try to get current position. But depending on the browser, code works differently:

  • in IE, successCallback is called with every F5 refres
  • in FF, I get successCallback or errorCallback with error code = 2 (POSITION_UNAVAILABLE)
  • in Chrome I get successCallback or... nothing. I wait and wait and nothing

Actually I need successCallback to be called in some consistent manner... Is there a change to achieve it?

Code snippet:

function successCallback(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    alert("Your location is: " + latitude + "," + longitude);
}

function errorCallback(error) {
    console.log(error);
}

if (Modernizr.geolocation) {
    navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { maximumAge: 0 });
    alert("geolocation is enabled");
} else {
    alert("geolocation is NOT enabled");
}

Quick note 1:

alert("geolocation is enabled");

is called every time.

Quick note 2:

setting:

maximumAge: 1

works better. Now Chrome & IE call successCallback every time. Only FF freaks me out, it calles successCallback randomly.

解决方案

Not much you can do about this. You're at the whim of the browser developers and how they attempt to figure out your location based on WiFi Access Point fingerprinting, or IP lookup.

Out of curiosity, I tested the code (http://jsfiddle.net/YbtSZ/2/) on the following browsers and found it worked inconsistently.

Mac 10.7.2

  • Chrome on Mac - works
  • FF 9 on Mac - works
  • Safari on Mac - does NOT work

XP native

  • FF 9 on XP - works
  • IE 8 on XP - does NOT work (Geolocation NOT supported)

XP running virtualised in Mac

  • Chrome on XP (virtualised in Mac) - does NOT work (POSITION UNAVAILABLE)
  • FF 9 on XP (virtualised in Mac) - does NOT work (POSITION_UNAVAILABLE)
  • IE 8 on XP (virtualised in Mac) - does NOT work (Geolocation NOT supported)

这篇关于HTML5 GeoLocation API - 不需要回叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆