濮阳杆衣贸易有限公司

主頁 > 知識庫 > 使用HTML5 Geolocation實現(xiàn)一個距離追蹤器

使用HTML5 Geolocation實現(xiàn)一個距離追蹤器

熱門標簽:黃島區(qū)地圖標注 南寧點撥外呼系統(tǒng)哪家公司做的好 云南大理400電話申請官方 當涂高德地圖標注 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 四川點撥外呼系統(tǒng) 電銷機器人電話用什么卡 成都智能外呼系統(tǒng)平臺 江蘇智能電銷機器人哪家好

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

頁面結(jié)構(gòu)如下所示:

<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">經(jīng)度:  </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加載時調(diào)用loadDemo()方法,方法根據(jù)navigator.geolocation來判斷瀏覽器是否支持HTML5 Geolocation;如果navigator.geolocation為true,那么我們就可以開始對用戶位置進行獲取更新

實時獲取用戶位置

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

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

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

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 = "經(jīng)度:  " + 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;

計算距離

把開始位置和當前位置的經(jīng)度緯度作為參數(shù)放入函數(shù),通過換算,來計算距離(單位為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,你需要注冊百度賬號,申請成為百度開發(fā)者然后獲取一個密鑰,才能使用相關(guān)服務(wù)戳這 根據(jù)文檔你可以知道如何使用這個API 代碼如下:

var map = new BMap.Map("allmap"); // 創(chuàng)建Map實例
 map.centerAndZoom(new BMap.Point(longitude, latitude), 14); //設(shè)置中心點坐標和地圖級別
 map.addControl(new BMap.MapTypeControl()); //添加地圖類型控件
 map.setCurrentCity("南昌"); //顯示城市,此項必須設(shè)置
 map.enableScrollWheelZoom(true); //開啟鼠標滾輪縮放
 // 以下為當前位置標注設(shè)置
 var point = new BMap.Point(longitude, latitude);
 map.centerAndZoom(point, 14);
 var marker = new BMap.Marker(point); //創(chuàng)建標注
 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>

總結(jié)

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

標簽:十堰 佳木斯 酒泉 廣西 淮安 咸寧 西寧 南京

巨人網(wǎng)絡(luò)通訊聲明:本文標題《使用HTML5 Geolocation實現(xiàn)一個距離追蹤器》,本文關(guān)鍵詞  使用,HTML5,Geolocation,實現(xià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)文章
  • 下面列出與本文章《使用HTML5 Geolocation實現(xiàn)一個距離追蹤器》相關(guān)的同類信息!
  • 本頁收集關(guān)于使用HTML5 Geolocation實現(xiàn)一個距離追蹤器的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    普兰店市| 德昌县| 凤庆县| 蚌埠市| 岑巩县| 五莲县| 定州市| 南溪县| 乌海市| 泰州市| 沁水县| 远安县| 平塘县| 营口市| 灌南县| 昌江| 红安县| 界首市| 斗六市| 海伦市| 绍兴市| 伊吾县| 新兴县| 汨罗市| 宜城市| 连云港市| 丘北县| 旅游| 双柏县| 遂平县| 乌拉特前旗| 萍乡市| 鹿泉市| 北川| 夹江县| 宁都县| 浦县| 陈巴尔虎旗| 山东| 天气| 广元市|