地理信息
- 定位用戶的位置,使用HTML5 Geolocation API用於獲得用戶的位置
- 但是必須要獲取用戶的同意,否則不行
- 主要使用navigator.geolocation.getCurrentPosition(success, error, options)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
const {longitude,latitude,accuracy,altitude,altitudeAccuracy,heading,speed} = position.coords
console.log(position)
}, function (error) {
alert(error.message)
}, {
enableHighAccuracy: false,
timeout: 8000,
maximumAge: 1000
})
} else {
alert("你的瀏覽器不支持Geolocation,建議升級")
}
</script>
</body>
</html>
重複性位置更新請求
navigator.geolocaton.watchPosition(請求成功,請求失敗,數據收集方式)
- 這個只會在移動設備上有用,位置改變纔會觸發
- 配置參數:frequency更新的頻率
- 使用clearWatch關閉更新請求
百度地圖api:http://lbsyun.baidu.com/
高德地圖api:http://lbs.amap.com/