html5指南-4.使用Geolocation實(shí)現(xiàn)定位功能
發(fā)布時(shí)間:2013-01-07 16:11:36 作者:佚名
我要評(píng)論

今天我們要學(xué)習(xí)的是使用Geolocation實(shí)現(xiàn)定位功能。我們可以通過(guò)navigator.geolocation獲取Geolocation對(duì)象,感興趣的朋友可以了解下
今天我們要學(xué)習(xí)的是使用Geolocation實(shí)現(xiàn)定位功能。我們可以通過(guò)navigator.geolocation獲取Geolocation對(duì)象,他提供了下列方法:
getCurrentPosition(callback,errorCallback,options):獲取當(dāng)前位置;
watchPosition(callback,error,options):開(kāi)始監(jiān)控當(dāng)前位置;
clearWatch(id):停止監(jiān)控當(dāng)前位置。
note:下面例子使用的瀏覽器是chrome,使用其他瀏覽器我不能保證運(yùn)行結(jié)果和例子顯示的結(jié)果一致。
1.獲取當(dāng)前位置
我們將使用getCurrentPosition方法獲取當(dāng)前位置,位置信息不會(huì)以結(jié)果的形式直接返回,我們需要使用callback函數(shù)進(jìn)行處理。在獲取坐標(biāo)的過(guò)程中會(huì)有些延遲,還會(huì)問(wèn)你要訪問(wèn)權(quán)限。我們來(lái)看下面的例子:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time 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對(duì)象包含兩個(gè)屬性,coords:返回坐標(biāo)信息;timestamp:獲取坐標(biāo)信息的時(shí)間。其中coords又包括下面屬性:latitude:緯度;longitude:經(jīng)度;altitude:高度;accuracy:精確度(米);altitudeAccuracy:高度精確度(米);heading:行進(jìn)方向;speed:行進(jìn)速度(米/秒)。
并不是所有的信息都會(huì)返回,這取決于你承載瀏覽器的設(shè)備。像有GPS、加速器、羅盤(pán)的移動(dòng)設(shè)備會(huì)返回大部分信息,家用電腦就不行了。家用電腦獲取的位置信息,取決于所處的網(wǎng)絡(luò)環(huán)境或者是wifi。下面我們看上例的運(yùn)行結(jié)果。
點(diǎn)擊允許,獲取坐標(biāo)信息。
現(xiàn)在我們介紹getCurrentPosition的異常處理,他是通過(guò)使用errorCallback回調(diào)函數(shù)實(shí)現(xiàn)的。函數(shù)返回的參數(shù)error包含兩個(gè)屬性,code:錯(cuò)誤類(lèi)型的代碼;message:錯(cuò)誤信息。code包含三個(gè)值:1:用戶沒(méi)有授權(quán)使用geolocation;2:無(wú)法獲取坐標(biāo)信息;3:獲取信息超時(shí)。
下面我們看個(gè)例子:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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>
拒絕授權(quán),運(yùn)行結(jié)果:
getCurrentPosition(callback,errorCallback,options)中的options有如下參數(shù)可以使用,enableHighAccuracy:使用最好的效果;timeout:超時(shí)時(shí)間(毫秒);maximumAge:指定緩存時(shí)間(毫秒)。我們來(lái)下下面的例子:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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.監(jiān)視位置變化
下面我們介紹使用watchPosition方法實(shí)現(xiàn)位置變化的監(jiān)視,他的使用方法和getCurrentPosition一樣。我們來(lái)看例子:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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>
當(dāng)點(diǎn)擊Cancel Watch按鈕時(shí),停止監(jiān)視。
demo下載地址:Html5Guide.Geolocation.zip
getCurrentPosition(callback,errorCallback,options):獲取當(dāng)前位置;
watchPosition(callback,error,options):開(kāi)始監(jiān)控當(dāng)前位置;
clearWatch(id):停止監(jiān)控當(dāng)前位置。
note:下面例子使用的瀏覽器是chrome,使用其他瀏覽器我不能保證運(yùn)行結(jié)果和例子顯示的結(jié)果一致。
1.獲取當(dāng)前位置
我們將使用getCurrentPosition方法獲取當(dāng)前位置,位置信息不會(huì)以結(jié)果的形式直接返回,我們需要使用callback函數(shù)進(jìn)行處理。在獲取坐標(biāo)的過(guò)程中會(huì)有些延遲,還會(huì)問(wèn)你要訪問(wèn)權(quán)限。我們來(lái)看下面的例子:
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time 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對(duì)象包含兩個(gè)屬性,coords:返回坐標(biāo)信息;timestamp:獲取坐標(biāo)信息的時(shí)間。其中coords又包括下面屬性:latitude:緯度;longitude:經(jīng)度;altitude:高度;accuracy:精確度(米);altitudeAccuracy:高度精確度(米);heading:行進(jìn)方向;speed:行進(jìn)速度(米/秒)。
并不是所有的信息都會(huì)返回,這取決于你承載瀏覽器的設(shè)備。像有GPS、加速器、羅盤(pán)的移動(dòng)設(shè)備會(huì)返回大部分信息,家用電腦就不行了。家用電腦獲取的位置信息,取決于所處的網(wǎng)絡(luò)環(huán)境或者是wifi。下面我們看上例的運(yùn)行結(jié)果。
點(diǎn)擊允許,獲取坐標(biāo)信息。
現(xiàn)在我們介紹getCurrentPosition的異常處理,他是通過(guò)使用errorCallback回調(diào)函數(shù)實(shí)現(xiàn)的。函數(shù)返回的參數(shù)error包含兩個(gè)屬性,code:錯(cuò)誤類(lèi)型的代碼;message:錯(cuò)誤信息。code包含三個(gè)值:1:用戶沒(méi)有授權(quán)使用geolocation;2:無(wú)法獲取坐標(biāo)信息;3:獲取信息超時(shí)。
下面我們看個(gè)例子:
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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>
拒絕授權(quán),運(yùn)行結(jié)果:
getCurrentPosition(callback,errorCallback,options)中的options有如下參數(shù)可以使用,enableHighAccuracy:使用最好的效果;timeout:超時(shí)時(shí)間(毫秒);maximumAge:指定緩存時(shí)間(毫秒)。我們來(lái)下下面的例子:
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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.監(jiān)視位置變化
下面我們介紹使用watchPosition方法實(shí)現(xiàn)位置變化的監(jiān)視,他的使用方法和getCurrentPosition一樣。我們來(lái)看例子:
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse: collapse;}
th, td{padding: 4px;}
th{text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Longitude:</th>
<td id="longitude">-</td>
<th>Latitude:</th>
<td id="latitude">-</td>
</tr>
<tr>
<th>Altitude:</th>
<td id="altitude">-</td>
<th>Accuracy:</th>
<td id="accuracy">-</td>
</tr>
<tr>
<th>Altitude Accuracy:</th>
<td id="altitudeAccuracy">-</td>
<th>Heading:</th>
<td id="heading">-</td>
</tr>
<tr>
<th>Speed:</th>
<td id="speed">-</td>
<th>Time Stamp:</th>
<td id="timestamp">-</td>
</tr>
<tr>
<th>Error Code:</th>
<td id="errcode">-</td>
<th>Error 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>
當(dāng)點(diǎn)擊Cancel Watch按鈕時(shí),停止監(jiān)視。
demo下載地址:Html5Guide.Geolocation.zip
相關(guān)文章
使用HTML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器
HTML5 Geolocation(地理定位)用于定位用戶的位置,那么怎么實(shí)現(xiàn)一個(gè)距離追蹤器呢?下面小編給大家?guī)?lái)了基于HTML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器的實(shí)例代碼,感興趣的朋2018-04-09Html5 Geolocation獲取地理位置信息實(shí)例
這篇文章主要介紹了Html5 Geolocation獲取地理位置信息實(shí)例,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-12-09HTML5的Geolocation地理位置定位API使用教程
地理位置(Geolocation)是 HTML5 的重要特性之一,提供了確定用戶位置的功能,借助這個(gè)特性能夠開(kāi)發(fā)基于位置信息的應(yīng)用,今天這篇文章就向大家介紹一下HTML5的Geolocation地理2016-05-12html5指南-7.geolocation結(jié)合google maps開(kāi)發(fā)一個(gè)小的應(yīng)用
今天我們將把html5的geolocation結(jié)合google maps開(kāi)發(fā)一個(gè)小的應(yīng)用,感興趣的朋友可以了解下,如有不足,愿大俠給予指教2013-01-07- Geolocation是HTML5標(biāo)準(zhǔn)下的一個(gè)Web API,利用它可以獲取設(shè)備的當(dāng)前位置信息(坐標(biāo)),本篇文章主要介紹了三個(gè)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-04