<body onLoad="loadDemo()">
<header>
<h1>oldmeter演示</h1>
<h4>距离跟踪器</h4>
</header>
<section>
<article>
<header>
<h1>你的位置</h1>
<p class="info" id="status">地理位置是不是在你的浏览器支持。</p>
<div class="geostatus">
<p id="latitude">纬度:</p>
<p id="longitude">经度:</p>
<p id="accuracy">精度:</p>
<p id="altitude">海拔:</p>
<p id="altitudeAccuracy">海拔精度:</p>
<p id="heading">行进方向、相对于正北:</p>
<p id="speed">速度(m/s):</p>
<p id="timestamp">时间戳:</p>
<p id="currDist">目前距离旅行:</p>
<p id="totalDist">总距离:</p>
</div>
</header>
</article>
</section>
<footer>
<h2>使用HTML5,和你的脚!</h2>
</footer>
<script type="text/javascript">
var totalDistance=0.0;
var lastLat;
var lastLong; Number.prototype.toRadians=function(){
return this * Math.PI/;
} function distance(latitude1,longitude1,latitude2,longitude2){
var R=;//R是地球半径,单位是km
var deltalatitude=(latitude2-latitude1).toRadians();
var deltaLongitude=(longitude2-longitude1).toRadians();
latitude1=latitude1.toRadians();
latitude2=latitude2.toRadians(); var a=Math.sin(deltalatitude/) *
Math.sin(deltalatitude/) +
Math.cos(latitude1) *
Math.cos(latitude2) *
Math.sin(deltaLongitude/) *
Math.sin(deltaLongitude/);
var c=*Math.atan2(Math.sqrt(a),Math.sqrt(-a));
var d=R*c;
return d; } function updateErrorStatus(message){
document.getElementById("status").style.background="papayaWhip";
document.getElementById("status").innerHTML="<strong>Error</strong>:"+message; } function updateStatus(message){
document.getElementById("status").style.background="paleGreen";
document.getElementById("status").innerHTML=message; } function loadDemo(){
//检查浏览器是否支持geolocation
if(navigator.geolocation){
document.getElementById("status").innerHTML="在你的浏览器支持HTML5 Geolocation";
navigator.geolocation.watchPosition(updateLocation,handleLocationError,{timeout:});
}
} function updateLocation(position){ var latitude=position.coords.latitude;//纬度
var longitude=position.coords.longitude;//经度
var accuracy=position.coords.accuracy;//精度(准确度)单位米
var altitude=position.coords.altitude;//海拔
var altitudeAccuracy=position.coords.altitudeAccuracy;//海拔精度 单位米
var heading=position.coords.heading;//行进方向、相对于正北
var speed=position.coords.speed;//速度m/s
var timestamp=position.timestamp;//时间戳 document.getElementById("latitude").innerHTML="北纬度:"+latitude;
document.getElementById("longitude").innerHTML="东经度:"+longitude;
document.getElementById("accuracy").innerHTML="精度:"+accuracy+"米";
document.getElementById("altitude").innerHTML="海拔:"+altitude+"米";
document.getElementById("altitudeAccuracy").innerHTML="海拔精度:"+altitudeAccuracy;
document.getElementById("heading").innerHTML="行进方向:"+heading;
document.getElementById("speed").innerHTML="速度:"+speed+"米";
document.getElementById("timestamp").innerHTML="时间戳:"+timestamp; //合理性检查...如果accuracy的值太大就不要计算距离了
if(accuracy>=){ updateStatus("需要更精确的值来计算距离");
return;
} if((lastLat !=null)&&(lastLong !=null)){
var currentDistance =distance(latitude,longitude,lastLat,lastLong); document.getElementById("currDist").innerHTML="目前距离旅行"+currentDistance.toFixed()+"km";
totalDistance +=currentDistance;
document.getElementById("totalDist").innerHTML="总距离"+currentDistance.toFixed()+"km"; updateStatus("位置成功更新。");
lastLong=longitude;
}
} //错误处理
function handleLocationError(error){
switch(error.code){
case :
updateErrorStatus("有一个错误在获取你的位置:错误信息"+error.message);
break;
case :
updateErrorStatus("用户选择不分享他或她的位置。");
break;
case :
updateErrorStatus("浏览器无法确定自己的位置,错误信息"+error.message);
break;
case :
updateErrorStatus("在检索位置之前,浏览器超时。");
break;
}
}
</script>
</body>

HTML5-新API-geolocation-实例-距离跟踪器的更多相关文章

  1. HTML5新api即pushState和replaceState实现无刷新修改url

    1,首先我面临一个需求,页面回退时需要知道来之前的页面状态.很简单,回退时在url里赋参数即可.问题是在ipad上,回退按钮是安卓那边的,我控制不了.只好采用js无刷新修改url历史记录,来告诉服务器 ...

  2. 用HTML5 Geolocation实现一个距离追踪器

    HTML5 Geolocation(地理定位)用于定位用户的位置.那么如何实现一个距离追踪器呢?我的思路是这样的,前提是浏览器支持h5地理定位,在这个基础上,获取用户位置,更新用户位置,计算距离,显示 ...

  3. 【转】odoo 新API装饰器中one、model、multi的区别

    http://blog.csdn.net/qq_18863573/article/details/51114893 1.one装饰器详解 odoo新API中定义方式: date=fields.Date ...

  4. HTML5新特性之移动设备API

    为了更好地为移动设备服务,HTML5推出了一系列针对移动设备的API. 1.Geolocation API Geolocation接口用于获取用户的地理位置.它使用的方法基于GPS或者其他机制(比如I ...

  5. HTML5新特性之CSS+HTML5实例

    1.新的DOCTYPE和字符集 HTML5的一项准则就是化繁为简,Web页面的DOCTYPE被极大的简化. <!DOCTYPE html> 同时字符集声明也被简化了: <meta c ...

  6. HTML5 程序设计 - 使用HTML5 Canvas API

    请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (45) ------ 第八章 POCO之获取原始对象与手工同步对象图和变化跟踪器

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 8-6  获取原始对象 问题 你正在使用POCO,想从数据库获取原始对象. 解决方案 ...

  8. HTML5新特性及详解

    什么是HTML5:HTML5 是下一代的HTML,将成为 HTML.XHTML 以及 HTML DOM 的新标准. 为 HTML5 建立的一些规则: 新特性应该基于 HTML.CSS.DOM 以及 J ...

  9. 通过新的 Azure 媒体服务资源管理器工具管理媒体工作流

    Xavier Pouyat    Azure 媒体服务高级项目经理 几个月前,一家广播公司找到了我,希望我向他们提供一种图形界面工具,好让他们使用 Azure媒体服务来上传.管理资产并对资产进行编 ...

随机推荐

  1. 如何实现一个malloc

    任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...

  2. MongoDB 3.0 导入命令

    在MongoDB的bin目录下执行 ./mongoimport -h 192.168.77.129 --db test --collection restaurants --drop --file / ...

  3. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

  4. NeHe OpenGL教程 第十七课:2D图像文字

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. [实变函数]2.5 Cantor 三分集

    1 Cantor 三分集的构造:                $$\bex P=\cap_{n=1}^\infty F_n.                   \eex$$ 2 Cantor 三分 ...

  6. centos7配置笔记

    原因:前两天服务器的硬盘出故障,报错:scsi 0:0:2:0: rejecting I/O to dead device,报这个错误的时候重启过一次,撑了一个月时间,现在直接导致整个文件系统崩溃.很 ...

  7. JAVA final关键字,常量的定义

    final(最终)是一个修饰符1.final可以修饰类,函数,变量(成员变量,局部变量)2.被final修饰后的类不可以被其它类继承3.被final修饰后的方法(函数)不可以被重写4.被final修饰 ...

  8. 树莓派安装3.5inch RPi LCD (A)显示屏

    3.5inch RPi LCD (A) 资料 产品介绍 用户手册 开发资料 开发软件 树莓派镜像 演示视频 FAQ 在自定义Raspbian系统镜像上怎么使用树莓派LCD? 先确保自定义镜像可正常进入 ...

  9. JS 中的 Window 对象

    窗口对象的属性和方法: 在js最外层写的function可以还可以理解为window对象的一个方法.定义的变量也可以称之为window对象的一个属性.例如:window.alert("--- ...

  10. Oracle 查看表空间的大小

    SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name FROM dba_free_space GROUP BY tables ...