geolocation——定位
PC——IP地址
精度比较低
IP库
Chrome -> Google
手机——GPS
window.navigator.geolocation
单次 getCurrentPosition(成功, 失败, 参数)
enableHighAccuracy 高精度模式——更慢、更费电
timeout 超时
maximumAge 缓存时间
结果:
latitude/longitude 纬度/经度
altitude 海拔高度
accuracy 精确度
altitudeAccuracy 高度精确度
heading 朝向
speed 速度
监听 watchPosition(成功, 失败, 参数)
demo;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
window.οnlοad=function (){
let oBtn=document.getElementById('btn1');
oBtn.οnclick=function (){
if(window.navigator.geolocation){
navigator.geolocation.getCurrentPosition(res=>{
alert('成功');
}, err=>{
alert('失败');
}, {
/*
enableHighAccuracy //高精度模式
timeout //超时时间
maximumAge //缓存
*/
});
}else{
alert('你的浏览器不支持定位');
}
};
};
</script>
<title></title>
</head>
<body>
<input type="button" name="" value="定位" id="btn1">
</body>
</html>