Html5 Geolocation demo
- <!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>°, <span id="startLon">???</span>°
- </p>
- <p>
- Current Location (lat, lon):<br/>
- <span id="currentLat">???</span>°, <span id="currentLon">???</span>°
- </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的更多相关文章
- HTML5 Geolocation API地理定位整理(二)
Geolocation 实例demo 1.使用watchPosition()监听客户端位置 var watchOne=null; if (navigator.geolocation) { //watc ...
- HTML5 Geolocation用来定位用户的位置。
HTML5 Geolocation用来定位用户的位置. 定位用户的位置 HTMl5 Geolocation API用来得到用户的地理位置. 由于这个可能和个人隐私相关.除非用户同意否则不能使用. 浏览 ...
- HTML5: HTML5 Geolocation(地理定位)
ylbtech-HTML5: HTML5 Geolocation(地理定位) 1.返回顶部 1. HTML5 Geolocation(地理定位) HTML5 Geolocation(地理定位)用于定位 ...
- HTML5 地理位置定位(HTML5 Geolocation)原理及应用
地理位置(Geolocation)是 HTML5 的重要特性之一,提供了确定用户位置的功能,借助这个特性能够开发基于位置信息的应用.今天这篇文章向大家介绍一下 HTML5 地理位置定位的基本原理及各个 ...
- 基于HTML5 geolocation 实现的天气预报功能
最近一直在学习HTML5,因为8月份要开发手机项目了.所以先把HTML5学习下. 基本思路: 1. 用户未设置任何城市之前,根据HTML5 geolocation 获取用户所在的地理位置. 2. 根据 ...
- HTML5 Geolocation 构建基于地理位置的 Web 应用
HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...
- html5 geolocation API
清单 1. 检查浏览器支持性if (navigator.geolocation) 清单 2. 单次定位请求 API void getCurrentPosition(updateLocation, op ...
- 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 ...
- html5 Geolocation(地理位置定位)学习
1.html5 Geolocation html5 Geolocation API 使用很简单,请求一个位置信息,如果用户同意,浏览器会返回一个位置信息,该位置是通过用户的底层设备(手机,电脑) 提供 ...
随机推荐
- Upgrading to Java 8——第四章 The Stream API
在这章中我们将学习Stream API,在JDK 8 中的一项新的特性.为了理解这一章的主题,你需要知道如何使用Lambda表达式和java.util.function里的预定义的函数式接口. 一个S ...
- An overview of the Spring MVC request flow
The Spring MVC request flow in short: When we enter a URL in the browser, the request comes to the d ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- SVN--(Eclipse)在历史记录中比较版本差异
前言 在SVN中比较各版本的差异是非常重要的功能. 方式 看图说话 结果
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- 【BZOJ】【2301】problem b
莫比乌斯反演/容斥原理 Orz PoPoQQQ PoPoQQQ莫比乌斯函数讲义第一题. for(i=1;i<=n;i=last+1){ last=min(n/(n/i),m/(m/i)); …… ...
- 微信连WiFi关注公众号流程更新 解决ios微信扫描二维码不关注就能上网的问题
前几天鼓捣了一下微信连WiFi功能,设置还蛮简单的,但ytkah发现如果是ios版微信扫描微信连WiFi生成的二维码不用关注公众号就可以直接上网了,而安卓版需要关注公众号才能上网,这样就少了很多ios ...
- CkEditor 插件开发
CKEditor的插件开发其实很简单只需要两步.1.通过CKEditor.plugins.add()方法编写插件的逻辑主体, 2.告诉CKEditor我们有一个自定义插件需要添加进来. //创建插件逻 ...
- 使用行为树(Behavior Tree)实现网游奖励掉落系统
原地址:http://blog.csdn.net/akara/article/details/6165421 [原创]使用行为树(Behavior Tree)实现网游奖励掉落系统by AKara 20 ...