地理位置反馈,同时接受请求 [英] Geolocation feedback while accepting the request

查看:99
本文介绍了地理位置反馈,同时接受请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地理位置的实施是相当不错的,只有几个步骤可以观察,但只有东西缺失,我猜。
我无法看到用户是否接受了请求(在获取位置对象之前),我不知道用户是否忽略了我的请求(在我的超时期间),或者请求是否丢失(以及失败回调不会被无故召唤)。



当用户接受请求时设置一个时间戳会很有用,我找不到任何能够给我的东西回复。

解决方案

基于我对新事物的理解,你需要这样的东西。
(经测试:在Opera - Works,Firefox 3.6和Chrome 8中 - 没有那么多(我需要更多时间进行调试))

场景:
页面尝试获取位置...但用户完全忽略提示,因此不存在(接受或拒绝),并且由于不会发送位置请求,所以也不会发生超时!
/ p>

基于此,您可能需要添加自己的逻辑来处理这种情况。为了这个例子,我要为我自己的包装器方法制作原型。 (挑剔的 - 我不是宽容使用全局等我只是想让一些工作)

  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']); //清除任何以前的计时器
}
var timeout = timeoutThreshold || 30000; // 30秒
window ['geolocationRequestTimeoutHandler'] = setTimeout('geolocationTimeoutHandler()',timeout); //设置超时处理程序
navigator.geolocation.getCurrentPosition(
function ){
clearTimeout(window ['geolocationRequestTimeoutHandler']);
successHandler(position);
},
function(error){
clearTimeout(window ['geolocationRequestTimeoutHandler ']);
errorHandler(error);
},
options
);
};
函数timeoutCallback(){
alert('您好!我们正在尝试找到您,但您尚未回答安全问题。\\\
\\\
请选择分享我的位置以使我们找到你。');
}
函数successCallback(position){
var msg ='';
msg + ='成功!你在: ';
msg + ='\\\
Latitude:'+ position.coords.latitude;
msg + ='\\\
Longitude:'+ position.coords.longitude;
msg + ='\\\
Altitude:'+ position.coords.altitude;
msg + ='\\\
Accuracy:'+ position.coords.accuracy;
msg + ='\\\
Heading:'+ position.coords.heading;
msg + ='\\\
Speed:'+ position.coords.speed;
alert(msg);
}
函数errorCallback(错误){
if(error.PERMISSION_DENIED){
alert(User denied access!);
} else if(error.POSITION_UNAVAILABLE){
alert(你必须隐藏在51区!);
} else if(error.TIMEOUT){
alert(嗯,我们超时了试图找到你隐藏的地方!);
}
}
navigator.geolocation.requestCurrentPosition(successCallback,errorCallback,timeoutCallback,7000,{maximumAge:10000,timeout:0});

这个概念是先设置一个定时器(如果没有设置,则默认为30秒)。如果用户在计时器到期之前没有做任何事情,则会调用timeoutCallback。



注释:


  1. 某些用户界面(例如iPhone / iPad / iPod Safari)可能会启用Allow / Deny提示模式 - 因此,建议离开这些用户,并让默认的UI处理事情

  2. 如果用户允许请求(迟到),超时可能会在响应回来之前触发 - 我不认为您可以对此进行任何操作

  3. 以上代码仅为示例...需要清理。


the geolocation implementation is quite good and got few steps to observe but only on thing is missing, i guess. Im not able to see if the user accepted the request or not ( before i get the position object ), i dunno if the user just ignores my request ( during my timeout ) or if the request just get lost ( and the failure callback doesnt get called for no reason ).

It would be useful to set a timestamp when the user accepts the request, i couldnt find anything which gives me that kind of response.

解决方案

Based on my new understanding of what you are after, you want something like this. (Tested: in Opera - works, Firefox 3.6 & Chrome 8 - not so much (I need more time to debug))

Scenario: Page attempts to get location... but user ignores the prompt completely thus there is no (accept or deny) and since the request for the location is never sent, there is no timeout either!

Based on this you may want to add your own logic to handle this scenario. For the sake of this example, I'm going to prototype my own "wrapper" method. (for the picky - I'm not condoning using globals etc. I was just trying to get something to work)

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});

The concept is to set up a timer first (defaults to 30 seconds if not set). If the user doesn't do anything before the timer expires, a timeoutCallback is called.

Notes:

  1. Some UI's (e.g. iPhone/iPad/iPod Safari) may make the Allow/Deny prompt modal - thus the user can't really continue until they pick something (I'd suggest to leave these users alone and let the default UI handle things
  2. If the user Allows the request (late), the timeout may still fire before the response comes back - I don't think there is anything you can do about this
  3. Code above is an example only... it needs cleaning up.

这篇关于地理位置反馈,同时接受请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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