V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wanderingFaker
V2EX  ›  问与答

关于 html5 获取定位

  •  
  •   wanderingFaker · Aug 16, 2016 · 3172 views
    This topic created in 3542 days ago, the information mentioned may be changed or developed.

    使用 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 replies    2016-08-16 14:20:46 +08:00
    cnqncom
        1
    cnqncom  
       Aug 16, 2016
    关注哈子,我好像没有遇到这样的情况
    DualWield
        2
    DualWield  
       Aug 16, 2016
    代码这样基本不会有人看了
    wanderingFaker
        3
    wanderingFaker  
    OP
       Aug 16, 2016
    @DualWield 为什么呢 小白一枚
    ziki
        4
    ziki  
       Aug 16, 2016
    刚好遇到这个问题,貌似是新版的 Chrome 只允许 localhost 域或者是走 https 的链接在允许获取地理位置信息
    fahai
        6
    fahai  
       Aug 16, 2016
    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  
       Aug 16, 2016
    @wanderingFaker 代码没有对齐
    zander1024
        8
    zander1024  
       Aug 16, 2016
    $(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  
       Aug 16, 2016
    诶 卧槽 明明格式化好的。。
    bdbai
        10
    bdbai  
       Aug 16, 2016 via Android
    @zander1024 用 Gist 或者各种 Paste
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2575 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    ♥ Do have faith in what you're doing.