接受请求时的地理位置反馈

发布于 2024-10-13 10:54:08 字数 166 浏览 5 评论 0原文

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

当用户接受请求时设置时间戳会很有用,我找不到任何可以给我这种响应的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏了南城 2024-10-20 10:54:08

根据我对你所追求的新的理解,你想要这样的东西。
(测试:在 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});

这个概念是首先设置一个计时器(如果未设置,则默认为 30 秒)。如果用户在计时器到期之前没有执行任何操作,则会调用 timeoutCallback。

注意:

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

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.
短暂陪伴 2024-10-20 10:54:08

它是 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! ");
    }
});

如果您指定 errorCallback..然后您可以跟踪用户是否拒绝提供访问权限。

可能的错误代码包括:

error.PERMISSION_DENIED    (numeric value 1)
error.POSITION_UNAVAILABLE (numeric value 2)
error.TIMEOUT              (numeric value 3)

It is part of the 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! ");
    }
});

If you specify the errorCallback... then you can track if the user has declined to provide access.

Possible error codes include:

error.PERMISSION_DENIED    (numeric value 1)
error.POSITION_UNAVAILABLE (numeric value 2)
error.TIMEOUT              (numeric value 3)
今天小雨转甜 2024-10-20 10:54:08

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

通过该解决方法,我们知道请求是否超时,即使之后调用成功/失败回调。

Tested it successful in 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');
}

With that workaround we know if the request timedout, even when the succeess / failure callback get called afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文