Javascript GeoLocation 在 Chrome 上不起作用

Javascript GeoLocation is not working on Chrome(Javascript GeoLocation 在 Chrome 上不起作用)
本文介绍了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 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)