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 的位置的更多相关文章

  1. WKWebView中HTML5获取位置失败

    WKWebView中HTML5获取位置失败,在info.plist文件中添加以下代码打开网页时就会询问是否允许获取位置信息了. <key>NSLocationAlwaysUsageDesc ...

  2. HTML5 Geolocation位置信息定位总结

    现在定位功能很常用,所以抽出一些时间将这个功能的知识总结一下作为知识梳理的依据.HTML5 Geolocation的定位用法很简单,首先请求位置信息,用户同意,则返回位置信息.HTML5 Geoloc ...

  3. html5获取位置信息,h5获取位置信息

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

  4. Web前端发展前景及就业方向

    Web前端发展前景及就业方向 HTML5技术已经日趋成熟 Html5是移动互联网前端的主流开发语言,目前还没有一个前端的开发语言能取代 html5的位置,所以说,无论你是做手机网站还是在手机app应用 ...

  5. 使用html5获取当前手机的经纬度,并接入百度地图API,查询出当前位置

    最近项目需要,稍微研究一下html5获取当前地理位置的问题. 获取当前位置的经纬度很简单,一句代码就搞定 navigator.geolocation.getCurrentPosition(functi ...

  6. HTML5调用百度地图API获取当前位置并直接导航目的地的方法

    <!DOCTYPE html> <html lang="zh-cmn-Hans">     <meta charset="UTF-8&quo ...

  7. 手机端网页使用html5地理定位获取位置失败的解决办法

    网上有很多关于html5 geolocation 获取地理定位的方法,我试了下,只有在IE edge浏览器可以成功获取到,在chrome,firefox,手机端的safari,QQ浏览器,微信浏览器, ...

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

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

  9. 根据HTML5 获取当前位置的经纬度【百度地图】【高德地图】

    是想让地图的定位用户位置更准确一些. 查看了介绍: http://www.w3school.com.cn/html5/html_5_geolocation.asp 看介绍中拿数据挺简单. <!D ...

随机推荐

  1. 一步一步学RenderMonkey(3)——改良Phong光照模型 【转】

    转载请注明出处: http://blog.csdn.net/tianhai110 改良后的Phong光照模型: 上一节实现的phong镜面光照模型,如果固定光源,移动视点(及matView 关联为ma ...

  2. JavaScript 中的不可变对象(Immutable Objects)

    默认情况下,JavaScript 中的对象是可变的.我们可以更改原始值(字符串,数字等)和对象.我们来看看这个对象: let obj = { num: , obj: { content: " ...

  3. EPF与Myeclipse 增强代码自动智能提示

    摘自: http://blog.csdn.net/ylchou/article/details/7639467 数字证书文件,导入用. EPF文件是著名的软件开发工具——Eclipse(IDE)的配置 ...

  4. python 实现计算数独

    输入文件格式: 008309100900060004007504800036000540001000600042000970005907300600010008004608200 输出结果: yuan ...

  5. sqlmap dvwa SQL Injection使用小记

    刚刚开始学习sql injection,初步使用sqlmap,使用 GET http://www.dvssc.com/dvwa/vulnerabilities/sqli/?id=1&Submi ...

  6. JDK自带内存及线程分析工具

    转载:http://blog.csdn.net/kimylrong/article/details/50970493 查询.监控.分析Java应用程序的时候,我们关注的点主要集中在资源利用,比如CPU ...

  7. 云计算之路-试用Azure:Azure内置的负载均衡是四层的

    当初迁移至阿里云遇到的第一个问题是在程序中通过Request.UserHostAddress得到的是负载均衡(SLB)的内网IP,而不是用户的真实IP.真实IP存储在服务器变量HTTP_X_Forwa ...

  8. poj 2253 (dis最短路径)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24979   Accepted: 8114 Descript ...

  9. Browsers 之 弹出窗口阻止问题

    主要关注两个地方: 1.Microsoft Edge 浏览器: 浏览器 “ 设置 → 查看高级设置 ”,找到 “ 阻止弹出窗口 ”,关闭. 2.IE浏览器 [1] “ 工具 → 弹出窗口阻止程序 ”, ...

  10. webpack+vuecli打包常见的2个坑

    第一个坑: 一般情况下,通过webpack+vuecli默认打包的css.js等资源,路径都是绝对的.但当部署到带有文件夹的项目中,这种绝对路径就会出现问题,因为把配置的static文件夹当成了根路径 ...