<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>HTML5 Geolocation距离跟踪器</title>
</head> <body onload="loadDemo()"> <h1>HTML5 Geolocation距离跟踪器</h1> <p id="status">该浏览器不支持HTML5 Geolocation。</p> <h2>当前位置:</h2>
<table border="1">
<tr>
<td width="40" scope="col">纬度</th>
<td width="114" id="latitude">?</td>
</tr>
<tr>
<td>经度</td>
<td id="longitude">?</td>
</tr>
<tr>
<td>准确度</td>
<td id="accuracy">?</td>
</tr>
</table> <h4 id="currDist">本次移动距离:0 千米</h4>
<h4 id="totalDist">总计移动距离:0 千米</h4> <script type="text/javascript"> var totalDistance = 0.0;
var lastLat;
var lastLong; function toRadians(degree) {
return this * Math.PI / 180;
} function distance(latitude1, longitude1, latitude2, longitude2) {
// R是地球半径(KM)
var R = 6371; var deltaLatitude = toRadians(latitude2-latitude1);
var deltaLongitude = toRadians(longitude2-longitude1);
latitude1 = toRadians(latitude1);
latitude2 = toRadians(latitude2); 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;
} function updateStatus(message) {
document.getElementById("status").innerHTML = message;
} function loadDemo() {
if(navigator.geolocation) {
updateStatus("浏览器支持HTML5 Geolocation。");
navigator.geolocation.watchPosition(updateLocation, handleLocationError, {maximumAge:20000});
}
} function updateLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy; document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;
document.getElementById("accuracy").innerHTML = accuracy; // 如果accuracy的值太大,我们认为它不准确,不用它计算距离
if (accuracy >= 500) {
updateStatus("这个数据太不靠谱,需要更准确的数据来计算本次移动距离。");
return;
} // 计算移动距离 if ((lastLat != null) && (lastLong != null)) {
var currentDistance = distance(latitude, longitude, lastLat, lastLong);
document.getElementById("currDist").innerHTML =
"本次移动距离:" + currentDistance.toFixed(4) + " 千米"; totalDistance += currentDistance; document.getElementById("totalDist").innerHTML =
"总计移动距离:" + currentDistance.toFixed(4) + " 千米";
} lastLat = latitude;
lastLong = longitude; updateStatus("计算移动距离成功。");
} function handleLocationError(error) {
switch(error.code)
{
case 0:
updateStatus("尝试获取您的位置信息时发生错误:" + error.message);
break;
case 1:
updateStatus("用户拒绝了获取位置信息请求。");
break;
case 2:
updateStatus("浏览器无法获取您的位置信息:" + error.message);
break;
case 3:
updateStatus("获取您位置信息超时。");
break;
}
} </script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#tripmeter {
border: 3px double black;
padding: 10px;
margin: 10px 0;
} p {
color: #222;
font: 14px Arial;
} span {
color: #00C;
}
</style>
</head>
<body>
<div id="tripmeter">
<p>
Starting Location (lat, lon):<br/>
<span id="startLat">???</span>&deg;, <span id="startLon">???</span>&deg;
</p>
<p>
Current Location (lat, lon):<br/>
<span id="currentLat">???</span>&deg;, <span id="currentLon">???</span>&deg;
</p>
<p>
Distance from starting location:<br/>
<span id="distance">0</span> km
</p>
</div>
<script>
window.onload = function() {
var startPos; if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
startPos = position;
document.getElementById("startLat").innerHTML = startPos.coords.latitude;
document.getElementById("startLon").innerHTML = startPos.coords.longitude;
}, function(error) {
alert("Error occurred. Error code: " + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from locaton provider)
// 3: timed out
}); navigator.geolocation.watchPosition(function(position) {
document.getElementById("currentLat").innerHTML = position.coords.latitude;
document.getElementById("currentLon").innerHTML = position.coords.longitude;
document.getElementById("distance").innerHTML =
calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
position.coords.latitude, position.coords.longitude);
});
}
}; // Reused code - copyright Moveable Type Scripts - retrieved May 4, 2010.
// http://www.movable-type.co.uk/scripts/latlong.html
// Under Creative Commons License http://creativecommons.org/licenses/by/3.0/
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
} </script>
</body>
</html>​

Html5 Geolocation demo的更多相关文章

  1. HTML5 Geolocation API地理定位整理(二)

    Geolocation 实例demo 1.使用watchPosition()监听客户端位置 var watchOne=null; if (navigator.geolocation) { //watc ...

  2. HTML5 Geolocation用来定位用户的位置。

    HTML5 Geolocation用来定位用户的位置. 定位用户的位置 HTMl5 Geolocation API用来得到用户的地理位置. 由于这个可能和个人隐私相关.除非用户同意否则不能使用. 浏览 ...

  3. HTML5: HTML5 Geolocation(地理定位)

    ylbtech-HTML5: HTML5 Geolocation(地理定位) 1.返回顶部 1. HTML5 Geolocation(地理定位) HTML5 Geolocation(地理定位)用于定位 ...

  4. HTML5 地理位置定位(HTML5 Geolocation)原理及应用

    地理位置(Geolocation)是 HTML5 的重要特性之一,提供了确定用户位置的功能,借助这个特性能够开发基于位置信息的应用.今天这篇文章向大家介绍一下 HTML5 地理位置定位的基本原理及各个 ...

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

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

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

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

  7. html5 geolocation API

    清单 1. 检查浏览器支持性if (navigator.geolocation) 清单 2. 单次定位请求 API void getCurrentPosition(updateLocation, op ...

  8. Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project

    Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project Qt WebKit and HTML5 geolocation   I ...

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

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

随机推荐

  1. Elasticseach部分语法总结

    索引 在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库 Relational DB -> D ...

  2. android 通过socket获取IP

    如题<android 通过socket获取IP>: socket.getInetAddress().getHostAddress();

  3. WEB学习总结 +数据结构

    HTML5  <h1>会员注册界面</h1><form action="process.aspx" method="post" n ...

  4. 创建REST服务应用程序

    Web服务类别有两种,一种是基于SOAP协议的服务,另一种是基于HTTP协议的REST架构风格的服务.REST服务的数据格式有两种:XML 和 JSON,REST服务已被大量应用于移动互联网中. 本文 ...

  5. max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...

  6. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB

    这个类型的resource 后面多个SRGB 表示SRGB空间是指gamma 矫正系数为2.2的色彩空间,这个问题我没有问别人,是我自己是上网查的,我觉得我不是个女人了......我又level up ...

  7. C编译错误解决方法

    1.expected identifier before numeric constant 一般情况下是枚举类型中的某个变量已经被#define定义过一次了,在项目空间中搜索你枚举类型中的所有变量类型 ...

  8. UML概述(转载)

    UML是一种标准语言,用于指定,可视化,构造和文档的软件系统. UML是OMG在1997年1月提出了创建由对象管理组织(OMG)和UML1.0规范草案. OMG不断努力,使一个真正的行业标准. UML ...

  9. Codeforces Round #271 (Div. 2) F ,E, D, C, B, A

    前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...

  10. rm删除命令

    linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有 ...