HTML5 Geolocation API 用于获得用户的地理位置。
鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的。
getCurrentPosition() 方法来获得用户的位置。
<script type="text/javascript">
var x = document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else { x.innerHTML = "Geolocation is not supported by this
browser."; }
}
function showPosition(position){
{
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
</script>
getLocation()检测是否支持地理定位
if (navigator.geolocation)如果支持,则运行 getCurrentPosition() 方法。
howPosition()函数获得并显示经度和纬度
function showPosition(position)如果getCurrentPosition()运行成功,则向参数showPosition中规定的函数返回一个coordinates对象
若定位成功,则 getCurrentPosition() 方法返回对象。始终会返回 latitude、longitude 以及 accuracy 属性。如果可用,则会返回其他下面的属性。
属性 | 描述 |
---|---|
coords.latitude | 十进制数的纬度 |
coords.longitude | 十进制数的经度 |
coords.accuracy | 位置精度 |
coords.altitudeAccuracy | 位置的海拔精度 |
coords.speed | 速度,以米/每秒计 |
coords.heading | 方向,从正北开始以度计 |
timestamp | 响应的日期/时间 |
©版权所有: 淮安深度人工智能科技有限公司2020, 备案号:苏ICP备2020056902号-2 | 技术支持:淮阴工学院计算机与软件工程学院《Web开发技术》教学团队