wanderingFaker
V2EX  ›  问与答

关于 html5 获取定位

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