天天看點

html自動擷取使用者位置,HTML5 - 使用Geolocation(地理定位)擷取使用者的位置

一、Geolocation(地理定位)

1,基本介紹

(1)雖然 Geolocation 經常以 HTML5 的名義提到,但地理定位實際上是一個單獨的标準,而且也不是經由 WHATWG 制定的。

(2)通過地理定位 Geolocation API 可以獲得使用者的地理位置(目前位置的地理坐标)。

注意:鑒于該特性可能侵犯使用者的隐私,除非使用者同意,否則使用者位置資訊是不可用的。

html自動擷取使用者位置,HTML5 - 使用Geolocation(地理定位)擷取使用者的位置

2,浏覽器支援情況

無論是桌面裝置,還是移動裝置。主流的浏覽器都是可以支援 Geolocation 的。注意 IE 浏覽器,是從 IE9 起開始支援 Geolocation。

二、擷取目前的定位資訊

1,getCurrentPosition()方法介紹

getCurrentPosition(geo_success, geo_error?, geo_options?)

該方法用于擷取目前的位置,其參數如下:

(1)geo_success:成功回調函數

(2)geo_error:失敗回調函數

(3)geo_options:傳遞的參數。其支援的屬性有:

timeout:指定擷取地理位置的逾時時間,預設不限時。機關為毫秒。

maximumAge:最長有效期,在重複擷取地理位置時,此參數指定多久再次擷取位置。預設為 0,表示浏覽器需要立刻重新計算位置。

enableHighAccuracy:訓示浏覽器擷取高精度的位置,預設為 false。當開啟後,可能沒有任何影響,也可能使浏覽器花費更長的時間擷取更精确的位置資料。

2,效果圖

點選“擷取位置”按鈕後,擷取目前裝置地理位置資訊,并顯示在頁面上。

html自動擷取使用者位置,HTML5 - 使用Geolocation(地理定位)擷取使用者的位置

3,樣例代碼

GPS

//顯示結果的區域對象

var result;

window.onload = function(){

result = document.getElementById("result")

}

//擷取位置

function getLocation(){

if( navigator.geolocation ){

navigator.geolocation.getCurrentPosition(

showPosition, handleLocationError,

{maximumAge:60000, timeout:50000, enableHighAccuracy:true}

);

}else{

result.innerHTML="該裝置不支援gps定位";

}

}

//擷取位置失敗處理

function handleLocationError(error) {

switch(error.code){

case 0:

alert("擷取位置資訊出錯!");

break;

case 1:

alert("您設定了阻止該頁面擷取位置資訊!");

break;

case 2:

alert("浏覽器無法确定您的位置!");

break;

case 3:

alert("擷取位置資訊逾時!");

break;

}

}

//顯示位置

function showPosition(position){

result.innerHTML="經度: " + position.coords.latitude +

"

緯度: " + position.coords.longitude +

"

準确度:" + position.coords.accuracy +

"

海拔:" + position.coords.altitude +

"

海拔準确度:" + position.coords.altitudeAccuracy +

"

時間戳:" + position.timestamp;

}

擷取位置

二、監視定位變化

1,watchPosition()方法介紹

如果我們需要設定一個回調函數來不斷響應定位資料發生的變更(裝置發生了移動,或擷取到了更高精度的地理位置資訊)。可以通過watchPosition() 函數實作該功能。

watchPosition() 與 getCurrentPosition() 接收的參數相同,但回調函數會被調用多次。

我們可以直接調用 watchPosition() 函數,不需要先調用 getCurrentPosition() 函數。

2,clearWatch()方法介紹

根據傳入的 watchid 來對應的位置監聽活動。

3,效果圖

(1)點選“開始監聽”按鈕後便開始監視裝置的位置變化,每當定位資訊有發生變化則會更新顯示目前位置資訊。

(2)點選“停止監聽”按鈕則停止對裝置位置變化的監視。

html自動擷取使用者位置,HTML5 - 使用Geolocation(地理定位)擷取使用者的位置

4,樣例代碼

GPS

//顯示結果的區域對象

var result;

//監聽定位的id

var watchID = null

window.onload = function(){

result = document.getElementById("result")

}

//開始監聽位置變化

function beginWatch(){

if( navigator.geolocation ){

watchID = navigator.geolocation.watchPosition(

showPosition, handleLocationError,

{maximumAge:60000, timeout:50000, enableHighAccuracy:true}

);

}else{

result.innerHTML="該裝置不支援gps定位";

}

}

//停止監聽位置變化

function stopWatch() {

navigator.geolocation.clearWatch(watchID);

}

//擷取位置失敗處理

function handleLocationError(error) {

switch(error.code){

case 0:

alert("擷取位置資訊出錯!");

break;

case 1:

alert("您設定了阻止該頁面擷取位置資訊!");

break;

case 2:

alert("浏覽器無法确定您的位置!");

break;

case 3:

alert("擷取位置資訊逾時!");

break;

}

}

//顯示位置

function showPosition(position){

result.innerHTML="經度: " + position.coords.latitude +

"

緯度: " + position.coords.longitude +

"

準确度:" + position.coords.accuracy +

"

海拔:" + position.coords.altitude +

"

海拔準确度:" + position.coords.altitudeAccuracy +

"

時間戳:" + position.timestamp;

}

開始監聽

停止監聽