编辑你的代码:
<!DOCTYPE html> <html> <body> <p id="demo">点击按钮获取经纬度坐标:</p> <button onclick="getLocation()">点击获取</button> <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>
查看结果:
phpxuexi.net
- 亲自试一试