为什么地理定位不支持我的浏览器?

时间:2017-10-01 19:39:57

标签: javascript node.js html5 mean-stack

为什么地理定位在我的浏览器中不支持? 我是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>

This is Output

2 个答案:

答案 0 :(得分:4)

您必须注意大写:将navigator.Geolocation)更改为navigator.geolocation

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

答案 1 :(得分:2)

JavaScript在变量名,API等方面区分大小写。使用没有大写G的地理位置。

MDN article on geolocation的示例:

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