主頁 > 知識庫 > html5定位并在百度地圖上顯示的示例

html5定位并在百度地圖上顯示的示例

熱門標(biāo)簽:硅谷的囚徒呼叫中心 阿里云 智能手機(jī) 美圖手機(jī) 檢查注冊表項(xiàng) 百度競價點(diǎn)擊價格的計算公式 使用U盤裝系統(tǒng) 網(wǎng)站建設(shè)

在開發(fā)移動端 web 或者webapp時,使用百度地圖 API 的過程中,經(jīng)常需要通過手機(jī)定位獲取當(dāng)前位置并在地圖上居中顯示出來,這就需要用到html5的地理定位功能。


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

navigator.geolocation.getCurrentPosition(callback);

在獲取坐標(biāo)成功之后會執(zhí)行回調(diào)函數(shù) callback; callback 方法的參數(shù)就是獲取到的坐標(biāo)點(diǎn);然后可以初始化地圖,設(shè)置控件、中心點(diǎn)、縮放等級,然后給地圖添加point的overlay:


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

var map = new BMap.Map("mapDiv");//mapDiv為放地圖的 div 的 id
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.centerAndZoom(point, 15);//point為坐標(biāo)點(diǎn),15為地圖縮放級別,最大級別是 18
var pointMarker = new BMap.Marker(point);
map.addOverlay(pointMarker);

然而事實(shí)上這樣還不夠,顯示出來的結(jié)果并不準(zhǔn),這是因?yàn)?getCurrentPosition 獲取到的坐標(biāo)是 GPS 經(jīng)緯度坐標(biāo),而百度地圖的坐標(biāo)是經(jīng)過特殊轉(zhuǎn)換的,所以,在獲取定位坐標(biāo)和初始化地圖之間需要進(jìn)行一步坐標(biāo)轉(zhuǎn)換工作,該轉(zhuǎn)換方法百度API里面已經(jīng)提供了,轉(zhuǎn)換一個點(diǎn)或者批量裝換的方法均有提供:單個點(diǎn)轉(zhuǎn)換需引用 http://developer.baidu.com/map/jsdemo/demo/convertor.js,批量轉(zhuǎn)換需引用 http://developer.baidu.com/map/jsdemo/demo/changeMore.js,這里只需要前者即可:


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

BMap.Convertor.translate(gpsPoint, 0, callback);
//gpsPoint:轉(zhuǎn)換前坐標(biāo),第二個參數(shù)為轉(zhuǎn)換方法,0表示gps坐標(biāo)轉(zhuǎn)換成百度坐標(biāo),callback回調(diào)函數(shù),參數(shù)為新坐標(biāo)點(diǎn)

例子的詳細(xì)代碼如下:(引用中的ak是申請的key)


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

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
*{
height: 100%; //設(shè)置高度,不然會顯示不出來
}
</style>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=··············"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<script>
$(function(){
navigator.geolocation.getCurrentPosition(translatePoint); //定位
});
function translatePoint(position){
var currentLat = position.coords.latitude;
var currentLon = position.coords.longitude;
var gpsPoint = new BMap.Point(currentLon, currentLat);
BMap.Convertor.translate(gpsPoint, 0, initMap); //轉(zhuǎn)換坐標(biāo)
}
function initMap(point){
//初始化地圖
map = new BMap.Map("map");
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.centerAndZoom(point, 15);
map.addOverlay(new BMap.Marker(point))
}
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>

本人開發(fā)過程中覺得電腦的定位速度有點(diǎn)慢,經(jīng)常無法獲取坐標(biāo)導(dǎo)致地圖無法顯示,建議用手機(jī)測試,定位較快。

當(dāng)然了,如果僅是開發(fā)移動端的網(wǎng)頁,就不需要使用jQuery,框架太大,可以換用其他輕量級的移動端的 js 框架。

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

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

    • 400-1100-266