好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > html5指南-4.使用Geolocation實現定位功能

html5指南-4.使用Geolocation實現定位功能

熱門標簽:地圖標注自己去過的地方 洛陽市伊川縣地圖標注中心官網 會聲會影怎樣做地圖標注效果 江蘇高頻外呼系統線路 平頂山電子地圖標注怎么修改 電銷機器人視頻 搜狗星級酒店地圖標注 標準智能外呼系統 高德地圖標注錯誤怎么修改
今天我們要學習的是使用Geolocation實現定位功能。我們可以通過navigator.geolocation獲取Geolocation對象,他提供了下列方法:
getCurrentPosition(callback,errorCallback,options):獲取當前位置;
watchPosition(callback,error,options):開始監控當前位置;
clearWatch(id):停止監控當前位置。
note:下面例子使用的瀏覽器是chrome,使用其他瀏覽器我不能保證運行結果和例子顯示的結果一致。
1.獲取當前位置
我們將使用getCurrentPosition方法獲取當前位置,位置信息不會以結果的形式直接返回,我們需要使用callback函數進行處理。在獲取坐標的過程中會有些延遲,還會問你要訪問權限。我們來看下面的例子:

復制代碼
代碼如下:

<!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對象包含兩個屬性,coords:返回坐標信息;timestamp:獲取坐標信息的時間。其中coords又包括下面屬性:latitude:緯度;longitude:經度;altitude:高度;accuracy:精確度(米);altitudeAccuracy:高度精確度(米);heading:行進方向;speed:行進速度(米/秒)。
并不是所有的信息都會返回,這取決于你承載瀏覽器的設備。像有GPS、加速器、羅盤的移動設備會返回大部分信息,家用電腦就不行了。家用電腦獲取的位置信息,取決于所處的網絡環境或者是wifi。下面我們看上例的運行結果。


點擊允許,獲取坐標信息。

2.處理異常
現在我們介紹getCurrentPosition的異常處理,他是通過使用errorCallback回調函數實現的。函數返回的參數error包含兩個屬性,code:錯誤類型的代碼;message:錯誤信息。code包含三個值:1:用戶沒有授權使用geolocation;2:無法獲取坐標信息;3:獲取信息超時。
下面我們看個例子:

復制代碼
代碼如下:

<!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>

拒絕授權,運行結果:

3.使用geolocation可選參數項
getCurrentPosition(callback,errorCallback,options)中的options有如下參數可以使用,enableHighAccuracy:使用最好的效果;timeout:超時時間(毫秒);maximumAge:指定緩存時間(毫秒)。我們來下下面的例子:

復制代碼
代碼如下:

<!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.監視位置變化
下面我們介紹使用watchPosition方法實現位置變化的監視,他的使用方法和getCurrentPosition一樣。我們來看例子:

復制代碼
代碼如下:

<!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>

當點擊Cancel Watch按鈕時,停止監視。
demo下載地址:Html5Guide.Geolocation.zip

標簽:蚌埠 廣西 廣東 阿克蘇 常德 鄂爾多斯 果洛 松原

巨人網絡通訊聲明:本文標題《html5指南-4.使用Geolocation實現定位功能》,本文關鍵詞  html5,指南,-4.,使用,Geolocation,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《html5指南-4.使用Geolocation實現定位功能》相關的同類信息!
  • 本頁收集關于html5指南-4.使用Geolocation實現定位功能的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 宝贝你那里又湿h| 99久久精品国产波多野结衣图片| 午夜精品福利视频| 惩罚屁股撅高扒开打臀缝| 99精品国产高清一区二区三区 | 性殴美| 国产精品欧美7777777| 国产精品一区二区欧美黑人| 城中村老熟妇???| japanese日本男同洲gay| 厨房婬乱公妇1-42小说| 国产丝袜| 日本乱码一卡二卡三卡永久| 欧美激情无码乱码精品一区三区| 久久香蕉精品| 国产天堂亚洲精品| 欧美黄色电影在线| 9制片厂制片传媒在线播放| 大学生A片一区二区三区| 被公疯狂献身的岬奈奈美359在线观看| 午夜看黄网站免费| 免费无遮挡??视频网站| 好吊妞国产欧美日韩在线视频| 欧美AV蜜桃一区二区蜜桃| 峡江县| 总裁跪趴高h秘书攻| 飘花伊人丝袜958| 久久人妻无码毛片A片麻豆| 男人用鸡鸡桶女人| 免费黄色软件下载大全| 无码人妻一区二区三区四区免费看| 成年美女黄网站小视频| 最新日韩中文字幕| 国产自产2023最新麻豆| 啊┅┅快┅┅用力啊岳视频| 久久96精品国产| 云南11选五5走势图| 乡村教师林越| 午夜免费片在线观看不卡| 5g影院午夜伴侣| 国产免费变态视频网址网站|