html5通过js来获取GPS地理定位

今天研究了下html5获取GPS地理位置,搞了半天获取不到,后来发现只有https网页才可以获取地理定位,可能是为了安全吧。下面给出html5 js获取GPS地理位置的代码。

if(navigator.geolocation)
{
    navigator.geolocation.getCurrentPosition(function(position){
        // 获取成功
        console.debug(position);
    }, function(err){
        // 获取失败
        console.debug(err);
    });
}
else
{
    console.debug('不支持获取GPS地理位置');
}

要测试的话一定要在https页面中才可以成功哦!这一点网上给出代码的网站都没说,坑死了!

最后再附上一个文档:https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation/getCurrentPosition

  • 标签
  • 发表评论
当前用户:
  • 评论列表
  • 最新文章
  • 最新评论