navigator.geolocation.getCurrentPosition(function(ev){
alert(ev.coords);//坐标(成功)
},function(ev){
ev.code //错误状态码(失败)
ev.message// 错误信息
});
ev.code 错误信息
0 未知错误
1 用户拒绝
2 获取失败
3.超时
getCurrentPosition :只能获取一次
watchPosition: 和定时器 一模一样,实时监控获取
document.addEventListener('DOMContentLoaded',function(){
var oBtn1 = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
var timer = null;
oBtn1.onclick = function(){
if(navigator.geolocation){
timer = navigator.geolocation.watchPosition(function(ev){ console.log(ev.coords.longitude);
console.log(ev.coords.latitude);
},
function(ev){
alert(ev.code+'--------message:'+ev.message);
});
}
};
oBtn2.onclick = function(){
navigator.geolocation.clearWatch(timer);
};
},false);