Web API接口之Geolocation
0、关于Geolocation
Geolocation,地理位置API。用于获取用户的位置信息。它不算是现有的HTML5标准的“直系”成员,但是是W3C的一个标准。它几乎就是一个真正的JavaScript API!
1、位置相关
1.1、经纬度表示位置
要想知道用户的位置,就需要有一个坐标系统,这就是我们的经纬度。一般我们用度/分/秒表示经纬度,如果需要将经纬度转换为小数,可以使用如下函数:
function degreesToDecimal(degrees, minutes, seconds){
return degrees + (minutes / 60) + (seconds / 3600);
}
1.2、API如何确定你的位置
浏览器要获取你的位置信息,并不要求你非得使用最新的智能手机,即使桌面浏览器也能获取你的位置。那么是如何获取到位置的呢?
其实,获取位置信息的方式有很多,比如:
- IP地址 --通过ip地址库获取你的位置
- GPS --通过全球定位系统获取你的位置(高精度)
- 蜂窝电话 --通过三角定位获取你的位置
- Wi-Fi -- 同样适用类似蜂窝电话的三角定位获取位置
我们没办法知道设备是使用的何种方法获取我们的位置信息,一些聪明的浏览器很可能会使用多种方式来确定你的位置。
2、如何使用Geolocation
在使用Geolocation之前,我们需要先是否支持,通过如下代码:
if(navigator.geolocation){
//Supported.
} else {
window.alert('No geolocation support.');
}
2.1、获取位置
获取位置信息的方法是一个异步方法,我们应该如下来使用它:
if(navigator.geolocation){
var callback = function(pos){
console.log('你的位置是:', pos);
};
navigator.geolocation.getCurrentPosition(callback);
} else {
window.alert('No geolocation support.');
}
其中pos长什么样呢?大概是如下这个样子的:
{
coords: { // Coordinates
accuracy: 137082,
altitude: null,
altitudeAccuracy: null,
heading: null,
latitude: 24.1848198,
longitude: 120.63149479999998,
speed: null
},
timestamp: 1453877895563
}
其中latitude和longitude就是我们的经纬度了。
该方法的语法是:navigator.geolocation.getCurrentPosition(success[, error[, options]]),具体参数含义,请接着往下看。
2.2、监控位置变化
在2.1中,我们知道如何获取位置,那如何监控位置变化呢?很容易相当的办法,就是我们定时去获取位置信息,然后比对。那么有没有更好的方式呢?当然,Geolocation API已经帮我们考虑好了。如下:
//正常时,会获取到一个地理位置信息
var watchSuccess = function(pos){
var latitude = pos.coords.latitude;
var longitude = pos.coords.longitude;
console.log('你的经纬度是:', latitude, longitude);
};
//错误时,函数会接收一个错误对象
var watchError = function(err){
console.warn(err, err.code, err.message);
};
var watcherId = navigator.geolocation.watchPosition(watchSuccess, watchError);
watchPosition方法的语法是:id = navigator.geolocation.watchPosition(success[, error[, options]])。所以,我们还可以针对这个方法设置参数:
var options = {
enableHighAccuracy: false, //默认false,为true时,则选择最高的精度获取位置
timeout: 5000, //每次获取位置信息的最长时间,默认是无限的
maximumAge: 0 // 缓存时间(毫秒),如果为0,则每次获取最新的
};
2.3 清除监控
既然我们有监控方法,那么该如何停止呢?这就要借助清除监控的方法了,代码如下:
navigator.geolocation.clearWatch(watcherId);
watcherId是2.2中监控时的返回值。
2.4、如何计算距离?
给予两个坐标点,如何计算两者之间的距离呢?一般采用半正矢(Haversine)公式,具体代码如下:
function degreesToRadians(degrees){ var radians = (degrees * Math.PI) / 180; return radians; }
function computeDistince(startCoords, destCoords){ var Radius = 6371; //每度在地球上的距离(km)
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);
var distince = Math.acos(
Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)
) * Radius;
return distince;
}
3、扩展
地理位置API单独使用意义不大,一般来说,结合地图就可以实现很复杂的功能了。
待续...
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
Web API接口之Geolocation的更多相关文章
- ASP.NET Web API 接口执行时间监控
软件产品常常会出现这样的情况:产品性能因某些无法预料的瓶颈而受到干扰,导致程序的处理效率降低,性能得不到充分的发挥.如何快速有效地找到软件产品的性能瓶颈,则是我们感兴趣的内容之一. 在本文中,我将解释 ...
- Web API接口之FileReader
Web API接口之FileReader *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- 不使用jQuery对Web API接口POST,PUT,DELETE数据
前些天,Insus.NET有演示Web API接口的操作: <怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html ...
- Winform混合式开发框架访问Web API接口的处理
在我的混合式开发框架里面,集成了WebAPI的访问,这种访问方式不仅可以实现简便的数据交换,而且可以在多种平台上进行接入,如Winform程序.Web网站.移动端APP等多种接入方式,Web API的 ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
回到目录 上一讲中介绍了使用HttpClient如何去调用一个标准的Web Api接口,并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对 ...
- Web API接口设计经验总结
在Web API接口的开发过程中,我们可能会碰到各种各样的问题,我在前面两篇随笔<Web API应用架构在Winform混合框架中的应用(1)>.<Web API应用架构在Winfo ...
- Web API 接口
Web API 接口 在给网站编写 JavaScript 代码时,也有很多可用的 API.您可以使用下面的接口(也称为对象的类型)列表,开发 Web 应用程序或网站. 关于包含这些接口的 API 列表 ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- 如何让你的 Asp.Net Web Api 接口,拥抱支持跨域访问。
由于 web api 项目通常是被做成了一个独立站点,来提供数据,在做web api 项目的时候,不免前端会遇到跨域访问接口的问题. 刚开始没做任何处理,用jsonp的方式调用 web api 接口, ...
随机推荐
- function变量困惑
var name = "The Window"; var object = { name : "My Object", getNameFunc : functi ...
- 谷歌浏览器允许ajax跨域以非安全模式打开
最近使用ajax的时候,因为是在本地测试调用 后台时一直会报错. 解决方案:用谷歌浏览器 以非安全的模式打开 在cmd命令行中 cd 到谷歌的安装目录下 (右键 属性 复制路径) 然后在 运行如下命令 ...
- 如果你想真正了解Struts2,不妨可以进来看看
首先我们就一起来认识认识Struts2到底是什么?作为框架,它又是用来处理哪些问题的呢?正所谓脚踏实地走,即时离梦想会远一点,但却很真实,那我们就一步一步的来了解Struts2吧! 一.既然 ...
- Java环境设置
win7/win8下JDK环境变量设置方法 首先需要到官网上下载JDK这款软件,本人下载的是jdk-7u40-windows-i586版本,安装完成显示jdk1.7.0_67. 其次选择安装路径.本人 ...
- 9.30notes
memcached 缓存机制,减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. array_slice(data['list'],0,10) ...
- 使用nmap工具查询局域网某个网段正在使用的ip地址
linux下nmap工具可扫描局域网正在使用的ip地址 查询局域网某网段正在使用的ip地址: nmap -sP .* 以上命令,将打印10.10.70.*/24网络所有正在使用的ip地址
- ASP.NET Core 文件上传
前言 上篇博文介绍了怎么样在 asp.net core 使用 Redis 和 Protobuf 进行 Session缓存.本篇的是开发过程中使用的一个小功能,怎么做单文件和多文件上传. 如果你觉得对你 ...
- ABP理论学习之验证DTO
返回总目录 本篇目录 验证介绍 使用数据注解 自定义验证 标准化 验证介绍 首先应该验证应用的输入.用户或者其它应用都可以向该应用发送输入.在一个web应用中,验证通常要实现两次:在客户端和服务器端. ...
- IDisposable的另类用法
IDisposable是.Net中一个很重要的接口,一般用来释放非托管资源,我们知道在使用了IDisposable的对象之后一定要调用IDisposable.Dispose()方法,或者使用.Net提 ...
- Hadoop学习笔记—14.ZooKeeper环境搭建
从字面上来看,ZooKeeper表示动物园管理员,这是一个十分奇妙的名字,我们又想起了Hadoop生态系统中,许多项目的Logo都采用了动物,比如Hadoop采用了大象的形象,所以我们可以猜测ZooK ...