您好,登錄后才能下訂單哦!
利用百度地圖API可以做很多事情,個人感覺最核心也是最基礎(chǔ)的就是定位功能了。這里分享一個制作的JS可以實現(xiàn)登錄網(wǎng)頁后定位:
<script type="text/javascript"> var map; var gpsPoint; var baiduPoint; var gpsAddress; var baiduAddress; var x; var y; function getLocation() { //根據(jù)IP獲取城市 var myCity = new BMap.LocalCity(); myCity.get(getCityByIP); //獲取GPS坐標(biāo) if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 }); } else { alert("您的瀏覽器不支持使用HTML 5來獲取地理位置服務(wù)"); } } function showMap(value) { var longitude = value.coords.longitude; var latitude = value.coords.latitude; map = new BMap.Map("map"); x=latitude; y=longitude; //alert("坐標(biāo)經(jīng)度為:" + latitude + ", 緯度為:" + longitude ); gpsPoint = new BMap.Point(longitude, latitude); // 創(chuàng)建點坐標(biāo) //根據(jù)坐標(biāo)逆解析地址 var geoc = new BMap.Geocoder(); geoc.getLocation(gpsPoint, getCityByCoordinate); BMap.Convertor.translate(gpsPoint, 0, translateCallback); map.enableScrollWheelZoom(true); } translateCallback = function (point) { baiduPoint = point; map.centerAndZoom(baiduPoint, 18); var geoc = new BMap.Geocoder(); geoc.getLocation(baiduPoint, getCityByBaiduCoordinate); } function getCityByCoordinate(rs) { gpsAddress = rs.addressComponents; var address = "GPS標(biāo)注:" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumber; var marker = new BMap.Marker(gpsPoint); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); marker.setLabel(labelgps); //添加GPS標(biāo)注 } function getCityByBaiduCoordinate(rs) { baiduAddress = rs.addressComponents; var address = "百度標(biāo)注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber; var marker = new BMap.Marker(baiduPoint); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); marker.setLabel(labelbaidu); //添加百度標(biāo)注 } //根據(jù)IP獲取城市 function getCityByIP(rs) { var cityName = rs.name; alert("根據(jù)IP定位您所在的城市為:" + cityName); } function handleError(value) { switch (value.code) { case 1: alert("位置服務(wù)被拒絕"); break; case 2: alert("暫時獲取不到位置信息"); break; case 3: alert("獲取信息超時"); break; case 4: alert("未知錯誤"); break; } } function init() { getLocation(); } window.onload = init; </script>
完成定位功能后可以添加相關(guān)代碼編輯地圖控件 覆蓋物 信息窗口等等各種功能。
附上百度地圖連接:http://lbsyun.baidu.com/
以上這篇利用百度地圖API獲取當(dāng)前位置信息的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。