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

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

问题描述

地理定位的实现非常好,需要观察的步骤很少,但我猜只是缺少一点.我无法看到用户是否接受了请求(在我获得位置对象之前),我不知道用户是否只是忽略了我的请求(在我的超时期间)或者请求是否丢失了(并且失败回调没有得到)无缘无故打电话).

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.

推荐答案

根据我对您的追求的新理解,您想要这样的东西.(已测试:在 Opera 中 - 有效,Firefox 3.6 & Chrome 8 - 没那么多(我需要更多时间来调试))

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.

Please choose "Share My Location" to enable us to find you.');
}
function successCallback(position){
  var msg = '';
  msg += 'Success! you are at: ';
  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);
}
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});

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

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.

注意事项:

  1. 某些用户界面(例如 iPhone/iPad/iPod Safari)可能会设置允许/拒绝提示模式 - 因此用户在他们选择某些内容之前无法真正继续(我建议让这些用户独自一人并让默认用户界面处理事情
  2. 如果用户允许请求(延迟),则在响应返回之前超时可能仍会触发 - 我认为您对此无能为力
  3. 上面的代码只是一个例子......它需要清理.

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

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