[html]代码库
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<script language="javascript">
var options = {
enableHighAccuracy: true,
maximumAge: 60000,
timeout: 45000
};
window.onload = function() {
if (window.navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback,options);
} else {
alert('Your browser does not natively support geolocation.');
}
}
function successCallback(position){
alert("成功获取地理位置...");
var posiText = "当前维度:"+position.coords.latitude;
posiText += "</br>" +"当前经度:"+position.coords.longitude;
posiText +="</br>" +"精确到:"+position.coords.accuracy +"米";
if (position.coords.altitude)
posiText += '</br>高度: ' + position.coords.altitude + " 米";
posiText += '<br/>距离北京天安门' + distance(position.coords.latitude,position.coords.longitude,39.5427,116.2317) +'公里';
document.getElementById("positionInfo").innerHTML = posiText;
}
function errorCallback(error){
}
function toRadians(degree) {
return degree * Math.PI / 180;
}
//通过经纬度计算距离的函数
function distance(latitude1, longitude1, latitude2, longitude2) {
// R is the radius of the earth in kilometers
var R = 6371;
var deltaLatitude = toRadians(latitude2-latitude1);
var deltaLongitude = toRadians(longitude2-longitude1);
latitude1 =toRadians(latitude1);
latitude2 =toRadians(latitude2);
var a = Math.sin(deltaLatitude/2) * Math.sin(deltaLatitude/2) +Math.cos(latitude1) *Math.cos(latitude2) * Math.sin(deltaLongitude/2) * Math.sin(deltaLongitude/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
</script>
</head>
<body>
当前位置信息<br/>
<div id ="positionInfo"></div>
</body>
</html>
[代码运行效果截图]