GeoLocation脚本不起作用?谁能帮助我代码中的错误]]

问题描述 投票:0回答:1

我想要当前位置的纬度和经度。

我的代码

    <script>
        var x = document.getElementById("demo");

        function getLocation() {
          if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
          } else { 
            x.innerHTML = "Geolocation is not supported by this browser.";
          }
        }

        function showPosition(position) {
          demo.innerHTML ="<form name='formname' action='weather.php' method='GET'> <input type='text' name='lat' id='lat' value='"+position.coords.latitude+"'><br><input type='text' name='long' id='long' value='"+position.coords.longitude+"'><input type='submit' name='submit' value='submit'/></form>";
        }
    </script>

<div>
    <button onclick="getLocation()" name="getlocation" id="getlocation" value="getlocation">Get location</button><br>

    <div id='demo'></div>
  </div>  

相同的代码在localhost中运行良好。但是在服务器上,它不起作用。

我想要当前位置的纬度和经度。我的代码] >>

只有在用户允许网站知道其位置的情况下,它才起作用。否则getCurrentPosition()将返回“未定义”

javascript html geolocation
1个回答
1
投票

只有在用户允许网站知道其位置的情况下,它才起作用。否则getCurrentPosition()将返回“未定义”

© www.soinside.com 2019 - 2024. All rights reserved.