【问题标题】:Why geolocation is not supporting in my any browser?为什么我的任何浏览器都不支持地理定位?
【发布时间】:2017-10-01 19:39:57
【问题描述】:

为什么我的任何浏览器都不支持地理定位? 我是 javascript 世界的新手。

这是我在节点 js 服务器上运行的代码。

<!DOCTYPE html>
<html>
<head>
    <title>Geolocation</title>
</head>
<body>
    <button onclick="getLocation()">Get coords</button>
    <h1 id="coords"></h1>
    <script>
        var x=document.getElementById('coords');
        function getLocation(){
            if (navigator.Geolocation) {
               navigator.Geolocation.getCurrentPosition(showPosition);
            }
        else{
            x.innerHTML="Geolocation is not supported";
        }
        function showPosition(position){
            console.log(position.coords.latitude);
        }
    }
     </script>
  </body>
</html>

【问题讨论】:

    标签: javascript node.js html mean-stack


    【解决方案1】:

    注意大小写:将navigator.Geolocation)改为navigator.geolocation

    MDN 上的地理定位 API 文档:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation

    【讨论】:

      【解决方案2】:

      JavaScript 在变量名、API 等方面区分大小写。使用不带大写 G 的地理位置。

      来自MDN article on geolocation的示例:

      if ("geolocation" in navigator) {
        /* geolocation is available */
      } else {
        /* geolocation IS NOT available */
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-23
        • 2020-09-20
        • 2012-02-23
        • 2019-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-24
        相关资源
        最近更新 更多