V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wanderingFaker
V2EX  ›  问与答

关于 html5 获取定位

  •  
  •   wanderingFaker · 2016-08-16 10:37:51 +08:00 · 2450 次点击
    这是一个创建于 2811 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 localhost 本机访问没问题。一旦换成 ip 地址访问就不行。报用户拒绝访问的错。丢到服务器上也是这样。、

    $(function(){ getLocation(); }); function getLocation(){
    if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }else{
    alert("浏览器不支持地理定位。");
    }
    }
    function showPosition(position){
    var lat = position.coords.latitude; //纬度
    var lag = position.coords.longitude; //经度
    alert('纬度:'+lat+',经度:'+lag);
    }
    function showError(error){
    switch(error.code) {
    case error.PERMISSION_DENIED:
    alert("定位失败,用户拒绝请求地理定位");
    break;
    case error.POSITION_UNAVAILABLE:
    alert("定位失败,位置信息是不可用");
    break;
    case error.TIMEOUT:
    alert("定位失败,请求获取用户位置超时");
    break;
    case error.UNKNOWN_ERROR:
    alert("定位失败,定位系统失效");
    break;
    }
    } 上代码 每次输出,用户拒绝请求地理定位

    10 条回复    2016-08-16 14:20:46 +08:00
    cnqncom
        1
    cnqncom  
       2016-08-16 10:43:09 +08:00
    关注哈子,我好像没有遇到这样的情况
    DualWield
        2
    DualWield  
       2016-08-16 11:04:05 +08:00
    代码这样基本不会有人看了
    wanderingFaker
        3
    wanderingFaker  
    OP
       2016-08-16 11:06:14 +08:00
    @DualWield 为什么呢 小白一枚
    ziki
        4
    ziki  
       2016-08-16 11:07:13 +08:00
    刚好遇到这个问题,貌似是新版的 Chrome 只允许 localhost 域或者是走 https 的链接在允许获取地理位置信息
    fahai
        6
    fahai  
       2016-08-16 11:13:54 +08:00
    getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.
    DualWield
        7
    DualWield  
       2016-08-16 11:20:22 +08:00
    @wanderingFaker 代码没有对齐
    zander1024
        8
    zander1024  
       2016-08-16 11:59:32 +08:00
    $(function() {
    getLocation();
    });
    function getLocation() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else {
    alert("浏览器不支持地理定位。");
    }
    }
    function showPosition(position) {
    var lat = position.coords.latitude; //纬度
    var lag = position.coords.longitude; //经度
    alert('纬度:' + lat + ',经度:' + lag);
    }
    function showError(error) {
    switch (error.code) {
    case error.PERMISSION_DENIED:
    alert("定位失败,用户拒绝请求地理定位");
    break;
    case error.POSITION_UNAVAILABLE:
    alert("定位失败,位置信息是不可用");
    break;
    case error.TIMEOUT:
    alert("定位失败,请求获取用户位置超时");
    break;
    case error.UNKNOWN_ERROR:
    alert("定位失败,定位系统失效");
    break;
    }
    }
    ------------------------------------------------
    我帮你格式化下。。
    zander1024
        9
    zander1024  
       2016-08-16 12:00:07 +08:00
    诶 卧槽 明明格式化好的。。
    bdbai
        10
    bdbai  
       2016-08-16 14:20:46 +08:00 via Android
    @zander1024 用 Gist 或者各种 Paste
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2914 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:49 · PVG 15:49 · LAX 00:49 · JFK 03:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.