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如何确定你的位置

浏览器要获取你的位置信息,并不要求你非得使用最新的智能手机,即使桌面浏览器也能获取你的位置。那么是如何获取到位置的呢?

其实,获取位置信息的方式有很多,比如:

  1. IP地址 --通过ip地址库获取你的位置
  2. GPS --通过全球定位系统获取你的位置(高精度)
  3. 蜂窝电话 --通过三角定位获取你的位置
  4. 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的更多相关文章

  1. ASP.NET Web API 接口执行时间监控

    软件产品常常会出现这样的情况:产品性能因某些无法预料的瓶颈而受到干扰,导致程序的处理效率降低,性能得不到充分的发挥.如何快速有效地找到软件产品的性能瓶颈,则是我们感兴趣的内容之一. 在本文中,我将解释 ...

  2. Web API接口之FileReader

    Web API接口之FileReader *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  3. 不使用jQuery对Web API接口POST,PUT,DELETE数据

    前些天,Insus.NET有演示Web API接口的操作: <怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html ...

  4. Winform混合式开发框架访问Web API接口的处理

    在我的混合式开发框架里面,集成了WebAPI的访问,这种访问方式不仅可以实现简便的数据交换,而且可以在多种平台上进行接入,如Winform程序.Web网站.移动端APP等多种接入方式,Web API的 ...

  5. WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递

    回到目录 上一讲中介绍了使用HttpClient如何去调用一个标准的Web Api接口,并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对 ...

  6. Web API接口设计经验总结

    在Web API接口的开发过程中,我们可能会碰到各种各样的问题,我在前面两篇随笔<Web API应用架构在Winform混合框架中的应用(1)>.<Web API应用架构在Winfo ...

  7. Web API 接口

    Web API 接口 在给网站编写 JavaScript 代码时,也有很多可用的 API.您可以使用下面的接口(也称为对象的类型)列表,开发 Web 应用程序或网站. 关于包含这些接口的 API 列表 ...

  8. Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口

    1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...

  9. 如何让你的 Asp.Net Web Api 接口,拥抱支持跨域访问。

    由于 web api 项目通常是被做成了一个独立站点,来提供数据,在做web api 项目的时候,不免前端会遇到跨域访问接口的问题. 刚开始没做任何处理,用jsonp的方式调用 web api 接口, ...

随机推荐

  1. iOS XMPP 通信协议实现 图形化直观感受

    第一次随笔,实在不知写点什么有用的东西,那就分享一下本人最近的研究所得吧! 是关于iOS-XMPP-通信协议的实现,具体代码比较复杂,三言两句也实在难表达清楚,网上已有很多关于iOS XMPP协议的讲 ...

  2. db2基础

    DB2知识文档 一.db2 基础 基本语法 注释:"--"(两个减号) 字符串连接:"||" 如set msg='aaaa'||'bbbb',则msg为'aaa ...

  3. xpath tutorial

    http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html http://www.w3schools.com/xpath/defaul ...

  4. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  5. css多行显示省略号

    首先说css多行显示省略号和单行文本省略号: 我们知道,单行显示省略号时,我们首先需要设置容器的宽度width:value(具体的值),然后强制文本在一行内显示,即white-spacing:nowr ...

  6. Torch7学习笔记(一)CmdLine

    该类主要为了提供一种方便解析参数的框架,对于每个实验尤其是神经网络中要调参数上.同时还可以把输出重定向到log文件中. 一般用法: cmd = torch.CmdLine() cmd:text() c ...

  7. Xamarin的不归路-生成安卓错误2

    错误提示: 解决方案:升级Andr​​oid sdk到Andr​​oid6.0即API23重新编译即可. Andr​​oid版本较低的手机如何使用? 你只需要编译和目标设定为6.0,根据需要设置最低A ...

  8. bzoj4325: NOIP2015 斗地主(爆搜+模拟)

    去年的我还不会打斗地主呵呵 觉得这道题挺难的..抄了一遍题解,感触挺多的= = 首先出牌的方式太多了不能每次都枚举所有的出牌方式, 于是分成两部分:1.顺子 2.带牌等其他 每次dfs都搜顺子,而且顺 ...

  9. Azure PowerShell (10) 使用PowerShell导出订阅下所有的Azure VM和Cloud Service的高可用情况

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China服务. 该脚本下载地址在http://files.cnblogs.co ...

  10. 跨域资源共享(CORS)在ASP.NET Web API中是如何实现的?

    在<通过扩展让ASP.NET Web API支持W3C的CORS规范>中,我们通过自定义的HttpMessageHandler自行为ASP.NET Web API实现了针对CORS的支持, ...