html5指南-4.使用Geolocation实现定位功能

2016-02-19 12:15 7 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的html5指南-4.使用Geolocation实现定位功能懂设计的网友们快点来了解吧!

【 tulaoshi.com - Web开发 】

今天我们要学习的是使用Geolocation实现定位功能。我们可以通过navigator.geolocation获取Geolocation对象,他提供了下列方法:
getCurrentPosition(callback,errorCallback,options):获取当前位置;
watchPosition(callback,error,options):开始监控当前位置;
clearWatch(id):停止监控当前位置。
note:下面例子使用的浏览器是chrome,使用其他浏览器我不能保证运行结果和例子显示的结果一致。
1.获取当前位置
我们将使用getCurrentPosition方法获取当前位置,位置信息不会以结果的形式直接返回,我们需要使用callback函数进行处理。在获取坐标的过程中会有些延迟,还会问你要访问权限。我们来看下面的例子:

代码如下:

!DOCTYPE HTML
html
head
titleExample/title
style
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
/style
/head
body
table border="1"
tr
thLongitude:/th
td id="longitude"-/td
thLatitude:/th
td id="latitude"-/td
/tr
tr
thAltitude:/th
td id="altitude"-/td
thAccuracy:/th
td id="accuracy"-/td
/tr
tr
thAltitude Accuracy:/th
td id="altitudeAccuracy"-/td
thHeading:/th
td id="heading"-/td
/tr
tr
thSpeed:/th
td id="speed"-/td
thTime Stamp:/th
td id="timestamp"-/td
/tr
/table
script
navigator.geolocation.getCurrentPosition(displayPosition);
function displayPosition(pos) {
var properties = ['longitude', 'latitude', 'altitude', 'accuracy', 'altitudeAccuracy', 'heading', 'speed'];
for (var i = 0, len = properties.length; i len; i++) {
var value = pos.coords[properties[i]];
document.getElementById(properties[i]).innerHTML = value;
}
document.getElementById('timestamp').innerHTML = pos.timestamp;
}
/script
/body
/html

返回的position对象包含两个属性,coords:返回坐标信息;timestamp:获取坐标信息的时间。其中coords又包括下面属性:latitude:纬度;longitude:经度;altitude:高度;accuracy:精确度(米);altitudeAccuracy:高度精确度(米);heading:行进方向;speed:行进速度(米/秒)。
并不是所有的信息都会返回,这取决于你承载浏览器的设备。像有GPS、加速器、罗盘的移动设备会返回大部分信息,家用电脑就不行了。家用电脑获取的位置信息,取决于所处的网络环境或者是wifi。下面我们看上例的运行结果。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)
点击允许,获取坐标信息。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)2.处理异常
现在我们介绍getCurrentPosition的异常处理,他是通过使用errorCallback回调函数实现的。函数返回的参数error包含两个属性,code:错误类型的代码;message:错误信息。code包含三个值:1:用户没有授权使用geolocation;2:无法获取坐标信息;3:获取信息超时。
下面我们看个例子:

代码如下:

!DOCTYPE HTML
html
head
titleExample/title
style
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
/style
/head
body
table border="1"
tr
thLongitude:/th
td id="longitude"-/td
thLatitude:/th
td id="latitude"-/td
/tr
tr
thAltitude:/th
td id="altitude"-/td
thAccuracy:/th
td id="accuracy"-/td
/tr
tr
thAltitude Accuracy:/th
td id="altitudeAccuracy"-/td
thHeading:/th
td id="heading"-/td
/tr
tr
thSpeed:/th
td id="speed"-/td
thTime Stamp:/th
td id="timestamp"-/td
/tr
tr
thError Code:/th
td id="errcode"-/td
thError Message:/th
td id="errmessage"-/td
/tr
/table
script
navigator.geolocation.getCurrentPosition(displayPosition, handleError);
function displayPosition(pos) {
var properties = ["longitude", "latitude", "altitude", "accuracy", "altitudeAccuracy", "heading", "speed"];
for (var i = 0; i properties.length; i++) {
var value = pos.coords[properties[i]];
document.getElementById(properties[i]).innerHTML = value;
}
document.getElementById("timestamp").innerHTML = pos.timestamp;
}
function handleError(err) {
document.getElementById("errcode").innerHTML = err.code;
document.getElementById("errmessage").innerHTML = err.message;
}
/script
/body
/html

拒绝授权,运行结果:

3.使用geolocation可选参数项
getCurrentPosition(callback,errorCallback,options)中的options有如下参数可以使用,enableHighAccuracy:使用最好的效果;timeout:超时时间(毫秒);maximumAge:指定缓存时间(毫秒)。我们来下下面的例子:

代码如下:

!DOCTYPE HTML
html
head
titleExample/title
style
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
/style
/head
body
table border="1"
tr
thLongitude:/th
td id="longitude"-/td
thLatitude:/th
td id="latitude"-/td
/tr
tr
thAltitude:/th
td id="altitude"-/td
thAccuracy:/th
td id="accuracy"-/td
/tr
tr
thAltitude Accuracy:/th
td id="altitudeAccuracy"-/td
thHeading:/th
td id="heading"-/td
/tr
tr
thSpeed:/th
td id="speed"-/td
thTime Stamp:/th
td id="timestamp"-/td
/tr
tr
thError Code:/th
td id="errcode"-/td
thError Message:/th
td id="errmessage"-/td
/tr
/table
script
var options = {
enableHighAccuracy: false,
timeout: 2000,
maximumAge: 30000
};
navigator.geolocation.getCurrentPosition(displayPosition, handleError, options);
function displayPosition(pos) {
var properties = ["longitude", "latitude", "altitude", "accuracy", "altitudeAccuracy", "heading", "speed"];
for (var i = 0; i properties.length; i++) {
var value = pos.coords[properties[i]];
document.getElementById(properties[i]).innerHTML = value;
}
document.getElementById("timestamp").innerHTML = pos.timestamp;
}
function handleError(err) {
document.getElementById("errcode").innerHTML = err.code;
document.getElementById("errmessage").innerHTML = err.message;
}
/script
/body
/html

4.监视位置变化
下面我们介绍使用watchPosition方法实现位置变化的监视,他的使用方法和getCurrentPosition一样。我们来看例子:

代码如下:

!DOCTYPE HTML
html
head
titleExample/title
style
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
/style
/head
body
table border="1"
tr
thLongitude:/th
td id="longitude"-/td
thLatitude:/th
td id="latitude"-/td
/tr
tr
thAltitude:/th
td id="altitude"-/td
thAccuracy:/th
td id="accuracy"-/td
/tr
tr
thAltitude Accuracy:/th
td id="altitudeAccuracy"-/td
thHeading:/th
td id="heading"-/td
/tr
tr
thSpeed:/th
td id="speed"-/td
thTime Stamp:/th
td id="timestamp"-/td
/tr
tr
thError Code:/th
td id="errcode"-/td
thError Message:/th
td id="errmessage"-/td
/tr
/table
button id="pressme"Cancel Watch/button
script
var options = {
enableHighAccuracy: false,
timeout: 2000,
maximumAge: 30000
};
var watchID = navigator.geolocation.watchPosition(displayPosition, handleError, options);
document.getElementById("pressme").onclick = function (e) {
navigator.geolocation.clearWatch(watchID);
};
function displayPosition(pos) {
var properties = ["longitude", "latitude", "altitude", "accuracy", "altitudeAccuracy", "heading", "speed"];
for (var i = 0; i properties.length; i++) {
var value = pos.coords[properties[i]];
document.getElementById(properties[i]).innerHTML = value;
}
document.getElementById("timestamp").innerHTML = pos.timestamp;
}
function handleError(err) {
document.getElementById("errcode").innerHTML = err.code;
document.getElementById("errmessage").innerHTML = err.message;
}
/script
/body
/html

当点击Cancel Watch按钮时,停止监视。
demo下载地址:Html5Guide.Geolocation.zip

来源:https://www.tulaoshi.com/n/20160219/1600372.html

延伸阅读
标签: Web开发
本小菜刚开始学习HTML5,现在对其中的Geolocation颇感兴趣,结合Google Map的API实现基本的地图定位功能。 1.获取当前地理位置 调用方法 void getCurrentPosition(onSuccess, onError, options);即可。 其中onSuccess是获取当前位置信息成功时执行的回调函数,onError是获取当前位置信息失败时所执行的回调函数,options是一些可选熟悉列表...
标签: Web开发
如果想使用该api,必须保证服务器端配置相应的mime类型。 拿tomcat为例,在打开Tomcat 6.0\conf\web.xml文件,在文件的最后面添加如下内容 代码如下: mime-mapping extensionmanifest/extension mime-typetext/cache-manifest/mime-type /mime-mapping 尤其注意,extension的value是manifest,所以以后配置缓存文件的后缀必须跟他一...
标签: Web开发
随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择。然而,每一款手机有不同的分辨率,不同屏幕大小,如何使我们开发出来的应用或页面大小能适合各种高端手机使用呢?学习html5 viewport的使用能帮你做到这一点 viewport 语法介绍: 代码如下: !-- html docu...
标签: Web开发
HTML5已经去掉name属性,实现锚点时请使用id,实例效果:http://keleyi.com/keleyi/phtml/html5/9.htm 支持Chrome,火狐,IE8以上等浏览器。 以下是完整代码: 代码如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1...
标签: Web开发
1、邮箱的自动验证 只需要type=email 2、日期的验证(年月日):type="date" 3、时间的验证(格式:00:00):type="time" 4、数字的验证 (可以向上加 向下减)type="number" 5、月份(--年--月)type="month" 6、周(--年--周)type="week" 7、range(范围0-100) type="range"...

经验教程

980

收藏

79
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部