HTML--獲取自己所在位置的經緯度

HTML--獲取自己所在位置的經緯度(HTML5)

注意:需要在能夠接受HTML5標準的瀏覽器上運行才能顯示

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>位置獲取</title>
</head>
<body>
<p id="location">點擊按鈕獲取當前座標:</p>
<button οnclick="getLocation()">請點擊</button>
<script>
var x=document.getElementById("location");
function getLocation()
{
if(navigator.geolocation)
	{
	   navigator.geolocation.getCurrentPosition(showPosition,showError);
	}
else
	{
	x.innerHTML="該瀏覽器版本不支持定位";
	}
}
function showPosition(position)//經緯度獲取
{
	x.innerHTML="緯度:"+position.coords.latitude+
	"<br>經度:"+position.coords.longitude;
}
function showError(error)//錯誤拋出
{
	switch(error.code)
	{
	case error.PERMISSION_DENIED:
	x.innerHTML="用戶拒絕對獲取地理位置的請求。"
	break;
	case error.POSITION_UNAVAILABLE:
		x.innerHTML="位置信息是不可用的。"
		break;
	case error.TIMEOUT:
		x.innerHTML="請求用戶地理位置超時。"
		break;
	case error.UNKNOWN_ERROR:
		x.innerHTML="未知錯誤。"
		break;
	}
}
</script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章