HTML5关于定位问题,高手请进

waxiao 2014-12-05 04:06:13
使用html5做了个应用,同时适应APP,微信和wap访问,其中有用到获取地理位置的相关内容,地理位置获取没有什么问题,现在问题是当获取地理位置询问用户时如果选择拒绝共享地理位置,那么下次再次登录进入时就不会再询问,导致如果拒绝了一次以后再也获取不到地理位置,不知道有什么方法可以解决?
获取地理位置代码如下:
//初始化时调用获取地理位置方法
$(function(){
startgps();
});

//获取地理位置方法
function startgps()
{
//判断是否支持
if (navigator.geolocation)
{
navigator.geolocation.watchPosition(showgps,
// navigator.geolocation.getCurrentPosition(showgps,//成功回调函数
function(error) //失败回调函数
{
alert("获取位置失败,开始执行失败回调函数");
getPositionError(error);
//showgps();
// showAlert("获取地理位置出错");
},
{enableHighAcuracy: true, timeout:1000,maximumAge: 0}); // 这里设置超时为1000毫秒,即1秒
}
else
{
showAlert("navigator.geolocation获取结果为false");
//showgps();
}
}

function showgps(position)
{
alert("获取位置成功,开始执行成功回调函数");
if (position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementsByName("lat")[0].value = latitude;
document.getElementsByName("lng")[0].value = longitude;
showAlert("获取到的地理位置为latitude:"+latitude+"---longitude="+longitude);
}
else{
showAlert("位置获取不到,请确认您打开了定位服务!");
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementsByName("lat")[0].value = latitude;
document.getElementsByName("lng")[0].value = longitude;
showAlert("定位服务没有打开时获取到的地理位置latitude:"+latitude+"---longitude="+longitude);
}
}

function getPositionError(error){
switch(error.code){

case error.TIMEOUT:

alert("连接超时,请重试");

break;

case error.PERMISSION_DENIED:

alert("您拒绝了使用位置共享服务,查询已取消");

break;

case error.POSITION_UNAVAILABLE:

alert("亲爱的火星网友,非常抱歉,我们暂时无法为您所在的星球提供位置服务");

break;

}
}
...全文
234 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sheic 2014-12-15
  • 打赏
  • 举报
回复
http://www.ibm.com/developerworks/cn/xml/x-gpswebapp/ 看下这个是不是有用?
sheic 2014-12-15
  • 打赏
  • 举报
回复
楼主,你那个返回的gps经纬度信息精确吗?
MaxCode-1 2014-12-15
  • 打赏
  • 举报
回复
写一个方法,当打开页面的时候清除一下缓存《注意一定要清除缓存,尤其是微信》,然后重新调用一下function startgps()这个方法

39,088

社区成员

发帖
与我相关
我的任务
社区描述
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
社区管理员
  • HTML5社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧