<p id="demo">点击这个按钮,获得您的坐标:</p>
<button onclick="getLocation()">试一下</button>
var x=document.getElementById("demo");
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition,showError);
else{x.innerHTML="Geolocation is not supported by this browser.";}
function showPosition(position)
x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
function showError(error)
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
x.innerHTML="The request to get user location timed out."
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."