国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

使用HTML5 Geolocation實現一個距離追蹤器

2024-09-05 07:22:28
字體:
來源:轉載
供稿:網友

HTML5 Geolocation(地理定位)用于定位用戶的位置。那么如何實現一個距離追蹤器呢?我的思路是這樣的,前提是瀏覽器支持h5地理定位,在這個基礎上,獲取用戶位置,更新用戶位置,計算距離,顯示到頁面,這樣就簡單實現了一個距離追蹤器,為了用戶更清楚地看到當前位置,這里接入了百度地圖API。

頁面結構如下所示:

<div id="container"> <section>  <article>   <header>    <h1>Your Location</h1>   </header>   <p class="info" id="status">您的瀏覽器不支持HTML5 Geolocation。</p>   <div class="geostatus">    <p id="latitude">緯度:  </p>    <p id="longitude">經度:  </p>    <p id="accuracy">準確度:  </p>    <p id="timestamp">時間戳:  </p>    <p id="currDist">目前旅行距離:  </p>    <p id="totalDist">旅行總距離:  </p>   </div>  </article> </section> <!-- 百度地圖位置顯示 --> <div id="allmap"></div>    </div>

判斷瀏覽器是否支持HTML5 Geolocation

在body加載時調用loadDemo()方法,方法根據navigator.geolocation來判斷瀏覽器是否支持HTML5 Geolocation;如果navigator.geolocation為true,那么我們就可以開始對用戶位置進行獲取更新

實時獲取用戶位置

HTML5可以通過getCurrentPosition() 方法來獲得用戶的位置。但這個只獲取一次,所以我們選用了 watchPosition()這個方法,它能返回用戶的當前位置,并繼續返回用戶移動時的更新位置(就像汽車上的GPS)。

navigator.geolocation.watchPosition(updateLocation, handleLocationError, {        timeout: 10000       });

在不斷獲取位置的同時,調用updateLocation這個方法,把位置情況顯示在頁面上,當然還要調用計算距離的方法來獲取距離,以及不斷更新百度地圖上的位置。

var latitude = position.coords.latitude;var longitude = position.coords.longitude;var accuracy = position.coords.accuracy;var timestamp = position.timestamp;document.getElementById("latitude").innerHTML = "緯度:  " + latitude;document.getElementById("longitude").innerHTML = "經度:  " + longitude;document.getElementById("accuracy").innerHTML = "準確度:  " + accuracy;document.getElementById("timestamp").innerHTML = "時間戳:  " + timestamp;if(accuracy >= 30000) { updateStatus("Need more accurate values to calculate distance."); return;}if((lastLat != null) && (lastLong != null)) { var currentDistance = distance(latitude, longitude, lastLat, lastLong); document.getElementById("currDist").innerHTML = "目前旅行距離:  " + currentDistance.toFixed(2) + "km"; totalDistance += currentDistance; document.getElementById("totalDist").innerHTML = "旅行總距離:  " + currentDistance.toFixed(2) + "km"; updateStatus("Location successfully updated.");}lastLat = latitude;lastLong = longitude;

計算距離

把開始位置和當前位置的經度緯度作為參數放入函數,通過換算,來計算距離(單位為km)

Number.prototype.toRadians = function() {    return this * Math.PI / 180;   }function distance(latitude1, longitude1, latitude2, longitude2) {    var R = 6371;    var deltaLatitude = (latitude2 - latitude1).toRadians();    var deltaLongitude = (longitude2 - longitude1).toRadians();    latitude1 = latitude1.toRadians(), latitude2 = latitude2.toRadians();    var a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) + Math.cos(latitude1) * Math.cos(latitude2) * Math.sin(deltaLongitude / 2) * Math.sin(deltaLongitude / 2);    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));    var d = R * c;    return d;   }

百度地圖API接入

要用百度地圖API,你需要注冊百度賬號,申請成為百度開發者然后獲取一個密鑰,才能使用相關服務戳這 根據文檔你可以知道如何使用這個API 代碼如下:

var map = new BMap.Map("allmap"); // 創建Map實例 map.centerAndZoom(new BMap.Point(longitude, latitude), 14); //設置中心點坐標和地圖級別 map.addControl(new BMap.MapTypeControl()); //添加地圖類型控件 map.setCurrentCity("南昌"); //顯示城市,此項必須設置 map.enableScrollWheelZoom(true); //開啟鼠標滾輪縮放 // 以下為當前位置標注設置 var point = new BMap.Point(longitude, latitude); map.centerAndZoom(point, 14); var marker = new BMap.Marker(point); //創建標注 map.addOverlay(marker); //將標注添加到地圖中 marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳動的動畫 // 百度地圖API功能--------end

記得先引入一個script標簽

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密鑰" ></script>

總結

以上所述是小編給大家介紹的使用HTML5 Geolocation實現一個距離追蹤器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安阳市| 浮梁县| 凭祥市| 江阴市| 武义县| 繁昌县| 当雄县| 闽清县| 巴楚县| 柘城县| 黎川县| 墨脱县| 宁乡县| 历史| 桐柏县| 水富县| 庆阳市| 兴安县| 专栏| 柳江县| 威宁| 依兰县| 嘉义市| 南澳县| 永仁县| 延边| 新乡县| 高青县| 清远市| 徐水县| 萝北县| 镇平县| 容城县| 玛沁县| 泰兴市| 武强县| 井冈山市| 娱乐| 辉南县| 桃源县| 刚察县|