1. 程式人生 > >html5 返回當前地理位置的坐標點(經緯度)

html5 返回當前地理位置的坐標點(經緯度)

aid ntb title meta script upd 獲取位置 谷歌地圖 get

BAIDU

<!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>

GOOGLE

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GIS開發利用html5獲取經緯度</title>
</head>

<body>
<span id="support">將以下的經緯度輸入谷歌地圖:</span>
<div id="show">
緯度:<span id="latitude"></span><br />
經度:<span id="longitude"></span><br />
經緯度準確度:<span id="accuracy"></span><br />
海拔:<span id="altitude"></span><br />
海拔高度的準確度:<span id="altitudeAcuracy"></span><br />
朝向:<span id="heading"></span><br />
速度:<span id="speed"></span>
</div>
<script type="text/javascript">
var doc = document,
    latitude = doc.getElementById(‘latitude‘),
    longitude = doc.getElementById(‘longitude‘),
    accuracy = doc.getElementById(‘accuracy‘),
    altitude = doc.getElementById(‘altitude‘),
	altitudeAcuracy = doc.getElementById(‘altitudeAcuracy‘),
	heading = doc.getElementById(‘heading‘),
	speed = doc.getElementById(‘speed‘),
	support = doc.getElementById(‘support‘),
    showDiv = doc.getElementById(‘show‘);
function lodeSupport(){
    if(navigator.geolocation){
        support.innerHTML = ‘將以下的經緯度輸入谷歌地圖(緯度 經度)查看自己位置:‘;
        showDiv.style.display = ‘block‘;
        navigator.geolocation.getCurrentPosition(updataPosition,showError);
    }else{
        support.innerHTML = ‘對不起,瀏覽器不支持。‘;
        showDiv.style.display = ‘none‘;
    }
}
function updataPosition(position){
    var latitudeP = position.coords.latitude,
        longitudeP = position.coords.longitude,
        accuracyP = position.coords.accuracy,
		altitudeP = position.coords.altitude,
		altitudeAcuracyP = position.coords.altitudeAcuracy,
		headingP = position.coords.heading,
		speedP = position.coords.speed;
    latitude.innerHTML = latitudeP;
    longitude.innerHTML = longitudeP;
    accuracy.innerHTML = accuracyP;
	altitude.innerHTML = altitudeP;
	altitudeAcuracy.innerHTML = altitudeAcuracyP;
	heading.innerHTML = headingP;
	speed.innerHTML = speedP;
}

function showError(error)
{
  switch(error.code)
    {
    case error.PERMISSION_DENIED:
      showDiv.innerHTML="用戶拒絕訪問地理位置"
      break;
    case error.POSITION_UNAVAILABLE:
      showDiv.innerHTML="地理位置信息無法獲取"
      break;
    case error.TIMEOUT:
      showDiv.innerHTML="獲取位置時間超時"
      break;
    case error.UNKNOWN_ERROR:
      showDiv.innerHTML="我擦。這是一個未知的錯誤"
      break;
    }
}

window.addEventListener(‘load‘, lodeSupport , true);
</script>
</body>
</html>



html5 返回當前地理位置的坐標點(經緯度)