Mae*_*rtz 9 permissions html5 geolocation
地理位置的实现是相当不错的,并且几乎没有什么步骤可以观察,但我猜这只是缺少的东西.我无法看到用户是否接受了请求(在我获得位置对象之前),我不知道用户是否只是忽略了我的请求(在我的超时期间)或者请求是否丢失(并且失败回调没有得到)无缘无故地说.
当用户接受请求时设置时间戳会很有用,我找不到任何能给我这种响应的东西.
它是Geolocation API的一部分:
// navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
navigator.geolocation.getCurrentPosition(
function(position){
//do something with position;
}, function(){
//handle condition where position is not available
//more specifically you can check the error code...
//error.code == 1
if(error.PERMISSION_DENIED){
alert("you denied me! ");
}
});
Run Code Online (Sandbox Code Playgroud)
如果指定errorCallback ...则可以跟踪用户是否拒绝提供访问权限.
可能的错误代码包括:
error.PERMISSION_DENIED (numeric value 1)
error.POSITION_UNAVAILABLE (numeric value 2)
error.TIMEOUT (numeric value 3)
Run Code Online (Sandbox Code Playgroud)
基于我对你所追求的事物的新认识,你想要这样的东西.(经测试:在Opera中工作,Firefox 3.6和Chrome 8 - 没那么多(我需要更多时间来调试))
场景: 页面尝试获取位置...但是用户完全忽略了提示,因此没有(接受或拒绝),因为从未发送过该位置的请求,也没有超时!
基于此,您可能希望添加自己的逻辑来处理此场景.为了这个例子,我将使用自己的"包装器"方法进行原型设计.(对于挑剔 - 我不是在使用全局变形等等.我只是想让一些东西起作用)
navigator.geolocation.requestCurrentPosition = function(successCB, errorCB, timeoutCB, timeoutThreshold, options){
var successHandler = successCB;
var errorHandler = errorCB;
window.geolocationTimeoutHandler = function(){
timeoutCB();
}
if(typeof(geolocationRequestTimeoutHandler) != 'undefined'){
clearTimeout(window['geolocationRequestTimeoutHandler']);//clear any previous timers
}
var timeout = timeoutThreshold || 30000;//30 seconds
window['geolocationRequestTimeoutHandler'] = setTimeout('geolocationTimeoutHandler()', timeout);//set timeout handler
navigator.geolocation.getCurrentPosition(
function(position){
clearTimeout(window['geolocationRequestTimeoutHandler']);
successHandler(position);
},
function(error){
clearTimeout(window['geolocationRequestTimeoutHandler']);
errorHandler(error);
},
options
);
};
function timeoutCallback(){
alert('Hi there! we are trying to locate you but you have not answered the security question yet.\n\nPlease choose "Share My Location" to enable us to find you.');
}
function successCallback(position){
var msg = '';
msg += 'Success! you are at: ';
msg += '\nLatitude: ' + position.coords.latitude;
msg += '\nLongitude: ' + position.coords.longitude;
msg += '\nAltitude: ' + position.coords.altitude;
msg += '\nAccuracy: ' + position.coords.accuracy;
msg += '\nHeading: ' + position.coords.heading;
msg += '\nSpeed: ' + position.coords.speed;
alert(msg);
}
function errorCallback(error){
if(error.PERMISSION_DENIED){
alert("User denied access!");
} else if(error.POSITION_UNAVAILABLE){
alert("You must be hiding in Area 51!");
} else if(error.TIMEOUT){
alert("hmmm we timed out trying to find where you are hiding!");
}
}
navigator.geolocation.requestCurrentPosition(successCallback, errorCallback, timeoutCallback, 7000, {maximumAge:10000, timeout:0});
Run Code Online (Sandbox Code Playgroud)
概念是首先设置计时器(如果未设置,则默认为30秒).如果用户在计时器到期之前没有执行任何操作,则会调用timeoutCallback.
笔记:
在FF 3.5,Opera 10.6,Chrome8,IE6-8中测试成功.
var succeed = function(obj) {
navigator.geolocation.received = true;
!navigator.geolocation.timedout?alert('GOT YAH'):alert('GOT YAH but user was to slow');
};
var failed = function(obj) {
navigator.geolocation.received = true;
!navigator.geolocation.timedout?alert('just failed'):alert('failed and user was to slow as well, tzz ._.');
};
var timedout = function() {
navigator.geolocation.timedout = true; // could be used for other callbacks to trace if its timed out or not
!navigator.geolocation.received?alert('Request timed out'):null;
}
// Extend geolocation object
if ( navigator.geolocation ) {
navigator.geolocation.retrievePermission = function retrievePermission(succeed,failed,options,timeout) {
this.received = false; // reference for timeout callback
this.timedout = false; // reference for other callbacks
this.getCurrentPosition.apply(this,arguments); // actual request
// Trigger timeout with its function; default timeout offset 5000ms
if ( timeout ) {
setTimeout(timeout.callback,timeout.offset || 5000);
}
}
// New location request with timeout callback
navigator.geolocation.retrievePermission(succeed,failed,{},{
offset: 10000, // miliseconds
callback: timedout
});
// Awesome thingy is not implemented
} else {
alert('geolocation is not supported');
}
Run Code Online (Sandbox Code Playgroud)
通过该解决方法,我们知道请求是否超时,即使后续调用后成功/失败回调.
归档时间: |
|
查看次数: |
4904 次 |
最近记录: |