微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

地理位置的实现是相当不错的,只有几件事情没有,我想.
我无法看到用户是否接受请求(在我获得位置对象之前),我不知道如果用户只是忽略我的请求(在我的超时期间),或者请求只是丢失(并且失败回调没有得到呼吁没有理由).

用户接受请求时设置时间戳是有用的,我找不到任何给我这种响应的东西.

解决方法

根据我对你以后的新了解,你想要这样的东西.
(测试:在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.

笔记:

>某些UI(例如iPhone / iPad / iPod Safari)可能会允许/拒绝提示模式 – 因此用户不能继续,直到他们选择某事(我建议将这些用户单独留下,让认UI处理事情>如果用户允许请求(延迟),超时可能会在响应回来之前触发 – 我不认为有什么可以做的>上面的代码只是一个例子…它需要清理.

原文地址:https://www.jb51.cc/html5/168082.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。