<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#text{width: 500px; height: 400px;}
</style>
</head>
<body>
<input type="button" value="单次地理位置定位" id="btn" /><br>
<textarea id="text"></textarea>
<script type="text/javascript">
window.onload = function(){
var oBtn = document.getElementById('btn');
var oText = document.getElementById('text');
/*navigator.geolocation
单次定位请求 :getCurrentPosition(请求成功,请求失败,数据收集方式)
请求成功函数
经度 : coords.longitude
纬度 : coords.latitude
准确度 : coords.accuracy
海拔 : coords.altitude
海拔准确度 : coords.altitudeAcuracy
行进方向 : coords.heading
地面速度 : coords.speed
时间戳 : new Date(position.timestamp)
*/
//保存多次地理位置定位的内容
var timer = null;
oBtn.onclick = function(){
alert('开始定位……');
//watchPosition 相当于 setInterval
timer = navigator.geolocation.watchPosition(function(position){//成功
alert('定位成功!');
oText.value += "经度:"+position.coords.longitude+'\n';
oText.value += "纬度:"+position.coords.latitude+'\n';
oText.value += "准确度:"+position.coords.accuracy+'\n';
oText.value += "时间戳:"+new Date(position.timestamp)+'\n';
},function(error){
alert(error.code);
//清除多次地理位置定位
navigator.geolocation.clearWatch(timer);
},{
/*数据收集 : json的形式
enableHighAcuracy : 更精确的查找,默认false
timeout :指定获取地理位置的超时时间,默认不限时,单位为毫秒
maximumAge :最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
*/
enableHighAccuracy:true,
maximumAge:2000
});
}
}
</script>
</body>
</html>