HTML5 的位置
HTML5 的位置
在HTML5COL学院的前面几个章节中我们已经对HTML5 Geolocation
API有了一定认识,接下来我们要对位置做些更有意思的处理;看看你与我们HTML5COL学院的办公室秘密位置相距多远。为此我们需要HTML5COL
学院的坐标,而且需要知道如何计算两个坐标之间的距离。
增加一个<div>
首先,在HTML中增加一个 <div>:
<!DOCTYPE html>
<html>
<head>
<meta='keywords' content='HTML5COL学院,HTML5COL,CSS3,HTML5COL,编码社区'>
</head>
<body>
<div id="location">
Your location will go here.
</div>
<div id="distance">
Distance from HTML5COL Office will go here.
</div>
</body>
</html>
计算距离
用半正矢(Haversine)公式计算两个坐标之间的距离,由于这个超出这一章的范畴,我们会给HTML5COL学院一些成品代码来完成这个计算:
function computeDistance(startCoords, destCoords) {
//这个函数取两个坐标,一个起点坐标,一个终点坐标,并返回二者之间的距离(单位为千米)
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);
var Radius = 6371; // radius of the Earth in km
var distance=Math.acos(Math.sin(startLatRads)*Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius;
return distance;
}
function degreesToRadians(degrees) {
//在HTML5COL学院‘画布’章节中还会看到更多地使用了这个函数
var radians = (degrees * Math.PI)/180;
return radians;
}
编写代码
既然有了一个函数来计算两个坐标之间的距离,下面我们来定义我们在HTML5COL学院总部办公室的位置(也是本课程作者所在的位置),也可输入以下代码:
var ourCoords = {
latitude: 47.624851,
longitude: -122.52099
};
现在来编写代码,我们所要做的是把你的位置和HTML5COL学院总部办公地的坐标传递到computeDistance函数:
function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
var km = computeDistance(position.coords, ourCoords);
//将你的位置坐标以及我们的坐标传递到computeDistance
var distance = document.getElementById("distance");
distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ";
//然后得到结果,并更新distance <div>的内容
}
代码总汇
<!DOCTYPE html>
<html>
<head> <meta='keywords' content='HTML5COL学院,HTML5COL,CSS3,HTML5COL,编码社区'>
<script>
window.onload=getMylocation;
var ourCoords = {
latitude: 47.624851,
longitude: -122.52099
}; function getMylocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(displayLocation,displayError); } else{
alert("Oops,no geolocation support!"); }
} function displayLocation(position){ var latitude=position.coords.latitude;
var longitude=position.coords.longitude;
var div=document.getElementById("location");
div.innerHTML="You are at Latitude:"+latitude+"Longitude:"+longitude; var km = computeDistance(position.coords, ourCoords);
var distance = document.getElementById("distance");
distance.innerHTML = "You are " + km + " km from the HTML5COL Office"; } function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude); var Radius = 6371; // radius of the Earth in km
var distance = Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius; return distance;
} function degreesToRadians(degrees) {
radians = (degrees * Math.PI)/180;
return radians;
} function displayError(error){ var errorTypes={
0: "Unkown error",
1: "Permission denied by user",
2: "Position is not available",
3: "Request timed out"
};
var errorMessage=errorTypes[error.code];
if(error.code==0 || error.code==2){
errorMessage=errorMessage+" "+error.message;
}
var div=document.getElementById("location");
div.innerHTML=errorMessage; }
</script> </head> <body>
<p>position:</p>
<div id="location" >
</div> <div id="distance" >
</div>
</body>
</html>
HTML5 的位置的更多相关文章
- WKWebView中HTML5获取位置失败
WKWebView中HTML5获取位置失败,在info.plist文件中添加以下代码打开网页时就会询问是否允许获取位置信息了. <key>NSLocationAlwaysUsageDesc ...
- HTML5 Geolocation位置信息定位总结
现在定位功能很常用,所以抽出一些时间将这个功能的知识总结一下作为知识梳理的依据.HTML5 Geolocation的定位用法很简单,首先请求位置信息,用户同意,则返回位置信息.HTML5 Geoloc ...
- html5获取位置信息,h5获取位置信息
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Web前端发展前景及就业方向
Web前端发展前景及就业方向 HTML5技术已经日趋成熟 Html5是移动互联网前端的主流开发语言,目前还没有一个前端的开发语言能取代 html5的位置,所以说,无论你是做手机网站还是在手机app应用 ...
- 使用html5获取当前手机的经纬度,并接入百度地图API,查询出当前位置
最近项目需要,稍微研究一下html5获取当前地理位置的问题. 获取当前位置的经纬度很简单,一句代码就搞定 navigator.geolocation.getCurrentPosition(functi ...
- HTML5调用百度地图API获取当前位置并直接导航目的地的方法
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8&quo ...
- 手机端网页使用html5地理定位获取位置失败的解决办法
网上有很多关于html5 geolocation 获取地理定位的方法,我试了下,只有在IE edge浏览器可以成功获取到,在chrome,firefox,手机端的safari,QQ浏览器,微信浏览器, ...
- HTML5 Geolocation用来定位用户的位置。
HTML5 Geolocation用来定位用户的位置. 定位用户的位置 HTMl5 Geolocation API用来得到用户的地理位置. 由于这个可能和个人隐私相关.除非用户同意否则不能使用. 浏览 ...
- 根据HTML5 获取当前位置的经纬度【百度地图】【高德地图】
是想让地图的定位用户位置更准确一些. 查看了介绍: http://www.w3school.com.cn/html5/html_5_geolocation.asp 看介绍中拿数据挺简单. <!D ...
随机推荐
- linux自定义开机启动服务和chkconfig使用方法
linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...
- linux:ls、ls -l、ls -al区别 示例
linux:ls.ls -l.ls -al区别 示例 比如test文件夹下有一个test文件.一个.文件夹.一个..文件夹. 则,执行三个命令后,显示效果如下: [root@linuxserver t ...
- 使用ant构建报错,编码GBK的不可映射字符解决方法
使用ant的核心就是这个build.xml.然后再在cmd中使用ant命令就行了. build.xml构建文件包含一个工程(project). 工程包含若干个目标(target). 目标可以依赖于其他 ...
- LINQ体验(6)——LINQ to SQL语句之Join和Order By
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系.多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中.分别为Join(Join查询), SelectM ...
- springMVC 头像裁剪上传并等比压
第一次写头像裁剪上传,原本想着直接本地预览裁剪再上传,可是时间有限,jquery.jcrop貌似并没有对 假设是ie下图片预览效果是滤镜做的 做出对应处理,也没有时间去改;仅仅好将就一下先把图片上传 ...
- CAD启动找不到AC1ST16.DLL
今天在安装Win7 x64上CAD2006启动报错:找不到ac1st16.dll文件. 一查,是系统变量的问题.在系统变量Path中cad的路径为: C:\Program Files (x86)\Co ...
- ionic 隐藏header-ionic中隐藏头部header
ionic 中隐藏头部header 通过 hide-nav-bar="true" 来实现 <ion-view hide-nav-bar="true"> ...
- winfrom cahce 问题
.Clear()不能随便用 .Clear()的比较没有什么意思,因为只是把DataTable清空而已,在堆中任然分配内存,一般要比较也是比较Close()方法,不过DataTable没有这个方法 至于 ...
- crc16 校验算法 C#
封装一个静态类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 【Android】Architecture Components最佳实践--Lifecycles
UI controllers (activities and fragments) 中代码越少越好,不应该自己去请求数据,而是用ViewModel来更新数据,并且监听LiveData来更新UI UI ...