地理位置在在线示例中不起作用?

时间:2019-06-25 01:21:34

标签: javascript html geolocation

这都是小提琴:

https://jsfiddle.net/a4q0zjm8/

以及此在线示例:

https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

代码:

<!DOCTYPE html>
<html>
    <body>
        <p id="demo">Click the button to get your coordinates.</p>
        <button onclick="getLocation()">Try It</button>
        <p/>
        <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) {
              x.innerHTML = "Latitude: " + position.coords.latitude + 
              "<br>Longitude: " + position.coords.longitude;
            }

        </script>    
    </body>
</html>

0 个答案:

没有答案