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)站的支持!