Web API接口之Geolocation

时间:2023-03-09 07:09:47
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如何确定你的位置

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

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

  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%
}
-->