佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > html5中地理位置定位api接口開發(fā)應(yīng)用小結(jié)

html5中地理位置定位api接口開發(fā)應(yīng)用小結(jié)

熱門標(biāo)簽:智能手機(jī) 使用U盤裝系統(tǒng) 美圖手機(jī) 硅谷的囚徒呼叫中心 阿里云 網(wǎng)站建設(shè) 檢查注冊(cè)表項(xiàng) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式
地理位置定位的幾種方式:IP地址,GPS,Wifi,GSM/CDMA

地理位置獲取流程
1、用戶打開需要獲取地理位置的web應(yīng)用。
2、應(yīng)用向?yàn)g覽器請(qǐng)求地理位置,瀏覽器彈出詢問,詢問用戶是否共享地理位置。
3、假設(shè)用戶允許,瀏覽器從設(shè)別查詢相關(guān)信息。
4、瀏覽器將相關(guān)信息發(fā)送到一個(gè)信任的位置服務(wù)器,服務(wù)器返回具體的地理位置。

HTML5地理地位的實(shí)現(xiàn)
1. 實(shí)現(xiàn)基于瀏覽器(無需后端支持)獲取用戶的地理位置技術(shù)
2. 精確定位用戶的地理位置( 精度最高達(dá)10m之內(nèi),依賴設(shè)備 )
3. 持續(xù)追蹤用戶的地理位置
4. 與 Google Map、或者 Baidu Map 交互呈現(xiàn)位置信息

Geolocation API 用于將用戶當(dāng)前地理位置信息共享給信任的站點(diǎn),這涉及用戶的隱私安全問題,所以當(dāng)一個(gè)站點(diǎn)需要獲取用戶的當(dāng)前地理位置,瀏覽器會(huì)提示用戶是“允許” or “拒絕”。
先看看哪些瀏覽器支持Geolocation API:
IE9.0+、FF3.5+、Safari5.0+、Chrome5.0+、Opera10.6+、IPhone3.0+、Android2.0+
Geolocation API存在于navigator對(duì)象中,只包含3個(gè)方法:

復(fù)制代碼
代碼如下:

1、getCurrentPosition //當(dāng)前位置
2、watchPosition //監(jiān)視位置
3、clearWatch //清除監(jiān)視
navigator.geolocation.getCurrentPosition( … , function(error){
switch(error.code){
case error.TIMEOUT :
alert( " 連接超時(shí),請(qǐng)重試 " );
break;
case error.PERMISSION_DENIED :
alert( " 您拒絕了使用位置共享服務(wù),查詢已取消 " );
break;
case error.POSITION_UNAVAILABLE :
alert( " ,抱歉,暫時(shí)無法為您所在的星球提供位置服務(wù) " );
break;
}
});

watchPosition像一個(gè)追蹤器與clearWatch成對(duì)。
watchPosition與clearWatch有點(diǎn)像setInterval和clearInterval的工作方式。
var watchPositionId = navigator.geolocation.watchPosition(success_callback, error_callback, options);
navigator.geolocation.clearWatch(watchPositionId );

HTML 5提供了地理位置等一系列API可以給用戶使用,方便用戶制作LBS的地理應(yīng)用,首先在支持HTML 5的瀏覽器中,當(dāng)開啟API時(shí),會(huì)詢問是否用戶同意使用api,否則不會(huì)開啟的,保證安全。
1、開啟,判斷是否瀏覽器支持LBS api

復(fù)制代碼
代碼如下:

function isGeolocationAPIAvailable()
{
var location = "No, Geolocation is not supported by this browser.";
if (window.navigator.geolocation) {
location = "Yes, Geolocation is supported by this browser.";
}
alert(location);
}

上面的例子中,還在displayError方法中,捕捉了異常;
2、獲得用戶的地理位置
這個(gè)使用getCurrentPosition就可以了;

復(fù)制代碼
代碼如下:

function requestPosition() {
if (nav == null) {
nav = window.navigator;
}
if (nav != null) {
var geoloc = nav.geolocation;
if (geoloc != null) {
geoloc.getCurrentPosition(successCallback);
}
else {
alert("Geolocation API is not supported in your browser");
}
}
else {
alert("Navigator is not found");
}
}

當(dāng)獲得地理位置成功后,會(huì)產(chǎn)生一個(gè)回調(diào)方法了,處理返回的結(jié)果,

復(fù)制代碼
代碼如下:

function setLocation(val, e) {
document.getElementById(e).value = val;
}
function successCallback(position)
{
setLocation(position.coords.latitude, "latitude"); setLocation(position.coords.longitude, "longitude");
}

3、一個(gè)很常見的問題,是如何跟蹤用戶不斷變化的地理位置,這里小結(jié)下其中用到的兩個(gè)api
1 watchPosition
例子如下:

復(fù)制代碼
代碼如下:

function listenForPositionUpdates() {
if (nav == null) {
nav = window.navigator;
}
if (nav != null) {
var geoloc = nav.geolocation;
if (geoloc != null) {
watchID = geoloc.watchPosition(successCallback);
} else {
alert("Geolocation API is not supported in your browser");
}
} else {
alert("Navigator is not found");
}
}

然后在successCallback中,就可以設(shè)置顯示最新的地理位置:

復(fù)制代碼
代碼如下:

function successCallback(position){
setText(position.coords.latitude, "latitude"); setText(position.coords.longitude, "longitude");
}

如果不希望實(shí)時(shí)跟蹤,則可以取消之:
function clearWatch(watchID) {
window.navigator.geolocation.clearWatch(watchID);
}
4、如何處理異常
當(dāng)遇到異常時(shí),可以捕捉之:

復(fù)制代碼
代碼如下:

if (geoloc != null) {
geoloc.getCurrentPosition(successCallback, errorCallback);
}
function errorCallback(error) {
var message = "";
switch (error.code) {
case error.PERMISSION_DENIED:
message = "This website does not have permission to use "
+ "the Geolocation API";
break;
case error.POSITION_UNAVAILABLE:
message = "The current position could not be determined.";
break;
case error.PERMISSION_DENIED_TIMEOUT:
message = "The current position could not be determined "
+ "within the specified timeout period.";
break;
}
if (message == "") {
var strErrorCode = error.code.toString();
message = "The position could not be determined due to "
+ "an unknown error (Code: " + strErrorCode + ").";
}
alert(message);
}

5、 在google 地圖上顯示位置(前提是有g(shù)oogle map api等設(shè)置好)

復(fù)制代碼
代碼如下:

function getCurrentLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showMyPosition,showError);
}
else
{
alert("No, Geolocation API is not supported by this browser.");
}
}
function showMyPosition(position)
{
var coordinates=position.coords.latitude+","+position.coords.longitude;
var map_url="http://maps.googleapis.com/maps/api/staticmap?center="
+coordinates+"&zoom=14&size=300x300&sensor=false";
document.getElementById("googlemap").innerHTML="<img src='"+map_url+"' />";
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
alert("This website does not have permission to use the Geolocation API")
break;
case error.POSITION_UNAVAILABLE:
alert("The current position could not be determined.")
break;
case error.TIMEOUT:
alert("The current position could not be determined within the specified time out period.")
break;
case error.UNKNOWN_ERROR:
alert("The position could not be determined due to an unknown error.")
break;
}
}

標(biāo)簽:湘潭 賀州 通遼 黃山 煙臺(tái) 山南 湖北 懷化

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5中地理位置定位api接口開發(fā)應(yīng)用小結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    仪征市| 新平| 永州市| 比如县| 蒙城县| 类乌齐县| 彭泽县| 土默特左旗| 梅州市| 博白县| 禹州市| 乌兰察布市| 南丰县| 红安县| 许昌市| 健康| 香港| 湄潭县| 巴青县| 醴陵市| 南宁市| 云安县| 宁晋县| 鱼台县| 崇阳县| 永平县| 页游| 满洲里市| 曲麻莱县| 阳泉市| 左贡县| 蒙自县| 西峡县| 杭锦后旗| 松桃| 胶南市| 金湖县| 故城县| 安西县| 北海市| 班玛县|