编程笔记 html5&css&js 035 HTML 地理定位
一、HTML5 地理定位二、处理错误和拒绝三、getCurrentPosition() 方法 - 返回数据四、Geolocation 对象 - 其他有趣的方法五、练习小结
地理定位有很多事情有关,购物时要知道你在哪吧,驾车时就更是了,所有浏览器还提供了定位的功能,可以在网页中使用。
一、HTML5 地理定位
HTML5 Geolocation(地理定位)用于定位用户的位置。 HTML5 Geolocation API 用于获得用户的地理位置。 鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的。 注释:对于拥有 GPS 的设备,比如 iPhone,地理定位更加精确。 请使用 getCurrentPosition() 方法来获得用户的位置。 下例是一个简单的地理定位实例,可返回用户位置的经度和纬度。
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 +
"
Longitude: " + position.coords.longitude;
}
检测是否支持地理定位 如果支持,则运行 getCurrentPosition() 方法。如果不支持,则向用户显示一段消息。 如果getCurrentPosition()运行成功,则向参数showPosition中规定的函数返回一个coordinates对象 showPosition() 函数获得并显示经度和纬度 上面的例子是一个非常基础的地理定位脚本,不含错误处理。
二、处理错误和拒绝
getCurrentPosition() 方法的第二个参数用于处理错误。它规定当获取用户位置失败时运行的函数: 实例 function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML=“User denied the request for Geolocation.” break; case error.POSITION_UNAVAILABLE: x.innerHTML=“Location information is unavailable.” break; case error.TIMEOUT: x.innerHTML=“The request to get user location timed out.” break; case error.UNKNOWN_ERROR: x.innerHTML=“An unknown error occurred.” break; } } Permission denied - 用户不允许地理定位 Position unavailable - 无法获取当前位置 Timeout - 操作超时
三、getCurrentPosition() 方法 - 返回数据
若成功,则 getCurrentPosition() 方法返回对象。始终会返回 latitude、longitude 以及 accuracy 属性。如果可用,则会返回其他下面的属性。
属性 描述
coords.latitude 十进制数的纬度
coords.longitude 十进制数的经度
coords.accuracy 位置精度
coords.altitude 海拔,海平面以上以米计
coords.altitudeAccuracy 位置的海拔精度
coords.heading 方向,从正北开始以度计
coords.speed 速度,以米/每秒计
timestamp 响应的日期/时间
四、Geolocation 对象 - 其他有趣的方法
watchPosition() - 返回用户的当前位置,并继续返回用户移动时的更新位置(就像汽车上的 GPS)。 clearWatch() - 停止 watchPosition() 方法 下面的例子展示 watchPosition() 方法。您需要一台精确的 GPS 设备来测试该例(比如 iPhone):
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.watchPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"
Longitude: " + position.coords.longitude;
}
五、练习
body {
color: cyan;
background-color: teal;
}
.container {
width: 500px; /* 设置容器的宽度 */
margin: 0 auto; /* 将左右边距设置为自动 */
line-height: 2;
}
点击这个按钮,获得您的地理定位:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
//coords.latitude 十进制数的纬度
// coords.longitude 十进制数的经度
// coords.accuracy 位置精度
// coords.altitude 海拔,海平面以上以米计
// coords.altitudeAccuracy 位置的海拔精度
// coords.heading 方向,从正北开始以度计
// coords.speed 速度,以米/每秒计
// timestamp 响应的日期/时间
function showPosition(position) {
x.innerHTML =
"纬度: " +
position.coords.latitude +
"
经度: " +
position.coords.longitude +
"
位置精度: " +
position.coords.accuracy +
"
海拔: " +
position.coords.altitude +
"
海拔精度: " +
position.coords.altitudeAccuracy +
"
方向: " +
position.coords.heading +
"
速度(米/秒): " +
position.speed +
"
定位时间: " +
position.timestamp;
}
小结
实际应用中还要结全地理应用,才能产生更为直观的感受。
精彩内容
发表评论