Javascript GeoLocation 在 Chrome 上不起作用

时间:2023-04-21
本文介绍了Javascript GeoLocation 在 Chrome 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试获取用户的地理位置,然后进行查询.在 Mozilla Firefox 中,它在 Safari 中也可以正常工作......但在 Chrome 中它根本不起作用.

I'm trying to take the geolocation of the User and then do a query. In Mozilla Firefox it works fine also in Safari.... but in Chrome it doesnt work at all.

window.onload = function(){
  if(navigator.geolocation){

    navigator.geolocation.getCurrentPosition(function(position){

        var latitude = position.coords.latitude,
            longitude = position.coords.longitude;
      console.log(latitude + " " + longitude);

    },handleError);

    function handleError(error){
      //Handle Errors
     switch(error.code) {
        case error.PERMISSION_DENIED:
            console.log("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            console.log("Location information is unavailable.");
            break;
        case error.TIMEOUT:
           console.log("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
           console.log("An unknown error occurred.");
            break;
    }
    }
  }else{
    // container.innerHTML = "Geolocation is not Supported for this browser/OS.";
    alert("Geolocation is not Supported for this browser/OS");
  }
};

我得到 error 1

User denied the request for Geolocation.

但我没有拒绝任何请求,实际上弹出窗口根本没有出现.

But i haven't denied any request, actually the popup window doesnt come up at all.

我已转到 Chrome 设置 > 显示高级 > 隐私 > 内容设置 > 位置允许所有人.

I've went to Chrome Settings > Show advanced > Privacy > Content Settings > Location allow for all.

重新启动 chrome 并没有发生任何事情.我确定我的代码是 100% 合法的,所以有人知道如何处理吗?谢谢!

Restarted chrome and nothing happened. I'm sure my code is 100% legit so does anyone know how to deal with it? Thanks!

推荐答案

好的,这很容易...... Chrome 自 2016 年 4 月 20 日起禁用了不安全网站的 Geolocation API(没有 https)......链接在这里

Ok this was quite easy... Chrome since 20 of April 2016 has disabled the Geolocation API for insecure websites (without https)... link here

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

所以别担心..

这篇关于Javascript GeoLocation 在 Chrome 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Firefox 的 geo.wifi.url 的 https://www.google.com/loc/json 没有使 下一篇:如何将 HTML5 地理位置数据保存到 python Django admin?

相关文章