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">纬度:&nbsp;&nbsp;</p>
<p id="longitude">经度:&nbsp;&nbsp;</p>
<p id="accuracy">准确度:&nbsp;&nbsp;</p>
<p id="timestamp">时间戳:&nbsp;&nbsp;</p>
<p id="currDist">目前旅行距离:&nbsp;&nbsp;</p>
<p id="totalDist">旅行总距离:&nbsp;&nbsp;</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 = "纬度:&nbsp;&nbsp;" + latitude;
document.getElementById("longitude").innerHTML = "经度:&nbsp;&nbsp;" + longitude;
document.getElementById("accuracy").innerHTML = "准确度:&nbsp;&nbsp;" + accuracy;
document.getElementById("timestamp").innerHTML = "时间戳:&nbsp;&nbsp;" + 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 = "目前旅行距离:&nbsp;&nbsp;" + currentDistance.toFixed(2) + "km";
totalDistance += currentDistance;
document.getElementById("totalDist").innerHTML = "旅行总距离:&nbsp;&nbsp;" + 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实现一个距离追踪器的更多相关文章

  1. HTML5编程之旅系列一:HTML5 Geolocation 初探

    让我们假设这样一个场景,有一个web应用程序,它可以向用户提供附近不远处某商场的打折优惠信息.使用HTML5 Geolocation API(地理定位 API),可以请求用户共享他们的位置信息. HT ...

  2. HTML5 Geolocation 构建基于地理位置的 Web 应用

    HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...

  3. HTML5-新API-geolocation-实例-距离跟踪器

    <body onLoad="loadDemo()"> <header> <h1>oldmeter演示</h1> <h4> ...

  4. html5 Geolocation(地理位置定位)学习

    1.html5 Geolocation html5 Geolocation API 使用很简单,请求一个位置信息,如果用户同意,浏览器会返回一个位置信息,该位置是通过用户的底层设备(手机,电脑) 提供 ...

  5. HTML5 Geolocation学习

    GeolocationAPI学习,我写的挺枯燥的,直接跳到最后看示例. 5.1 位置信息 HTML5 Geolocation API的使用方法相当简单.请求一个位置信息,如果用户同意,浏览器就会返回位 ...

  6. Html5 Geolocation demo

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>HTML5 Geoloca ...

  7. 『转』市售热门可穿戴式“活动追踪器 Tracker”导购指南

    充足的运动对于幸福的生活来说是至关重要的,但很多人总是找不到足够的时间运动,所以远远达不到日常需要的运动标准.这就是为什么今天活动跟踪器成为了热门话题,尤其是对于注重健康的消费者来说.除BodyMed ...

  8. Blazor Bootstrap 组件库地理定位/移动距离追踪组件介绍

    地理定位/移动距离追踪组件 通过浏览器 API 获取定位信息 DEMO https://www.blazor.zone/geolocations 小提示 注意: 出于安全考虑,当网页请求获取用户位置信 ...

  9. 基于HTML5 geolocation 实现的天气预报功能

    最近一直在学习HTML5,因为8月份要开发手机项目了.所以先把HTML5学习下. 基本思路: 1. 用户未设置任何城市之前,根据HTML5 geolocation 获取用户所在的地理位置. 2. 根据 ...

随机推荐

  1. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  2. [多校联考2019(Round 5 T2)]蓝精灵的请求(二分图染色+背包)

    [多校联考2019(Round 5)]蓝精灵的请求(二分图染色+背包) 题面 在山的那边海的那边住着 n 个蓝精灵,这 n 个蓝精灵之间有 m 对好友关系,现在蓝精灵们想要玩一个团队竞技游戏,需要分为 ...

  3. Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式

    Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...

  4. 转载 PowerDesigner导出mysql数据结构

    转自:https://blog.csdn.net/dkingyaoyao/article/details/84586146 好久没有使用PowerDesigner,突然想用它导出数据结构,居然忘记了. ...

  5. es7.2版本安装ik分词

    (一)到官网下载https://github.com/medcl/elasticsearch-analysis-ik对应版本的ik(直接下载releases版本,避免maven打包!!!如果不是这个版 ...

  6. vue梳理(2)

    -app.vue作为根组件被挂载到index.html文件里,其他的所有组件都是在app.vue组件里做文章. 展示给用户的就是app.vue里的内容,你觉得删的没有内容了但实际还有很多是因为什么呢? ...

  7. express与koa对比

    使用体验koaconst Koa = require('koa');const app = new Koa();app.use(ctx => { ctx.body = 'Hello Koa'; ...

  8. linux Apache 的安装

    rpm –qa httpd 查询是否安装了 Apache rpm –e 包名卸载安装程序 rpm –e --nodeps 包名卸载安装程序不产生依赖 #检查是否安装有依赖库 yum install – ...

  9. flask框架上下文

    上下文 上下文:相当于一个容器,保存了 Flask 程序运行过程中的一些信息. Flask中有两种上下文,请求上下文和应用上下文 请求上下文(request context) 在 flask 中,可以 ...

  10. java ThreadGroup源码分析

    使用: import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction; public class Test { public s ...