Html5 Geolocation獲取地理位置信息實(shí)例
Html5中提供了地理位置信息的API,通過瀏覽器來獲取用戶當(dāng)前位置?;诖颂匦钥梢蚤_發(fā)基于位置的服務(wù)應(yīng)用。在獲取地理位置信息前,首先瀏覽器都會(huì)向用戶詢問是否愿意共享其位置信息,待用戶同意后才能使用。
Html5獲取地理位置信息是通過Geolocation API提供,使用其getCurrentPosition方法,此方法中有三個(gè)參數(shù),分別是成功獲取到地理位置信息時(shí)所執(zhí)行的回調(diào)函數(shù),失敗時(shí)所執(zhí)行的回調(diào)函數(shù)和可選屬性配置項(xiàng)。
如下Demo演示了通過Geolocation獲取地理位置信息,并在百度地圖上顯示當(dāng)前位置(通過調(diào)用百度地圖API)。實(shí)驗(yàn)結(jié)果發(fā)現(xiàn)位置被定位到了大學(xué)城內(nèi)環(huán)東四路入口處,與本人所在位置(華工學(xué)生宿舍)偏差還是有點(diǎn)大的,達(dá)到200-300米左右。

代碼如下所示(其中convertor.js為百度地圖提供的坐標(biāo)轉(zhuǎn)化文件):
<!DOCTYPE html>
<html>
<head>
<title>H5地理位置Demo</title>
<script src="http://api.map.baidu.com/api?v=1.3" type="text/javascript">
</script>
<script type="text/javascript" src="convertor.js">
</script>
</head>
<body>
<div id="map" style="width:600px; height:400px">
</div>
</body>
<script type="text/javascript">
if (window.navigator.geolocation) {
var options = {
enableHighAccuracy: true,
};
window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options);
} else {
alert("瀏覽器不支持html5來獲取地理位置信息");
}
function handleSuccess(position){
// 獲取到當(dāng)前位置經(jīng)緯度 本例中是chrome瀏覽器取到的是google地圖中的經(jīng)緯度
var lng = position.coords.longitude;
var lat = position.coords.latitude;
// 調(diào)用百度地圖api顯示
var map = new BMap.Map("map");
var ggPoint = new BMap.Point(lng, lat);
// 將google地圖中的經(jīng)緯度轉(zhuǎn)化為百度地圖的經(jīng)緯度
BMap.Convertor.translate(ggPoint, 2, function(point){
var marker = new BMap.Marker(point);
map.addOverlay(marker);
map.centerAndZoom(point, 15);
});
}
function handleError(error){
}
</script>
</html>
convertor.js文件:
(function() { // 閉包
function load_script(xyUrl, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = xyUrl;
// 借鑒了jQuery的script跨域方法
script.onload = script.onreadystatechange = function() {
if ((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
callback && callback();
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if (head && script.parentNode) {
head.removeChild(script);
}
}
};
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
head.insertBefore(script, head.firstChild);
}
function translate(point, type, callback) {
var callbackName = 'cbk_' + Math.round(Math.random() * 10000); // 隨機(jī)函數(shù)名
var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from=" + type
+ "&to=4&x=" + point.lng + "&y=" + point.lat
+ "&callback=BMap.Convertor." + callbackName;
// 動(dòng)態(tài)創(chuàng)建script標(biāo)簽
load_script(xyUrl);
BMap.Convertor[callbackName] = function(xyResult) {
delete BMap.Convertor[callbackName]; // 調(diào)用完需要?jiǎng)h除改函數(shù)
var point = new BMap.Point(xyResult.x, xyResult.y);
callback && callback(point);
}
}
window.BMap = window.BMap || {};
BMap.Convertor = {};
BMap.Convertor.translate = translate;
})();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用HTML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器
HTML5 Geolocation(地理定位)用于定位用戶的位置,那么怎么實(shí)現(xiàn)一個(gè)距離追蹤器呢?下面小編給大家?guī)砹嘶贖TML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器的實(shí)例代碼,感興趣的朋2018-04-09HTML5的Geolocation地理位置定位API使用教程
地理位置(Geolocation)是 HTML5 的重要特性之一,提供了確定用戶位置的功能,借助這個(gè)特性能夠開發(fā)基于位置信息的應(yīng)用,今天這篇文章就向大家介紹一下HTML5的Geolocation地理2016-05-12html5指南-4.使用Geolocation實(shí)現(xiàn)定位功能
今天我們要學(xué)習(xí)的是使用Geolocation實(shí)現(xiàn)定位功能。我們可以通過navigator.geolocation獲取Geolocation對(duì)象,感興趣的朋友可以了解下2013-01-07html5指南-7.geolocation結(jié)合google maps開發(fā)一個(gè)小的應(yīng)用
今天我們將把html5的geolocation結(jié)合google maps開發(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

