The geolocation object

geolocation API建立在navigator.geolocation 上。

如果对象存在,才可以使用定位服务。

if ("geolocation" in navigator) {
/* geolocation is available */
} else {
/* geolocation IS NOT available */
}

Note: 在Firefox24以及更加老的版本中,"geolocation" in navigator 总是返回true不管是否可以使用定位服务。在firefox25以后修复了这个问题。

Getting the current position

使用getCurrentPositin()来获取当前的地理位置。

这是一个监听地理位置的异步请求,当定位被授权,进行第一个回调函数。你可以设置第二个参数作为错误函数回调。你可以设置一个最大访问定位的时间作为第三个请求。

Note: 默认的,getCurrentPosition()会尽快的返回位置,所以常常以较低的精确度。有GPS的设备常常会花费更多的时间来返回较高精确度的地理位置,所以,在这些设备上使用getCurrentPosition()时返回的是根据IP亦或WIFI定位的信息。

navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});

当定位成功的时候将会执行do_something函数

Watching the current position

如果位置信息改变(设备的移动亦或提升地理位置的准确度),你可以设置一个调用更新的位置信息的函数。

这个函数就是watchPosition(),和getCurrentPosition()函数的输入参数一样。

回调函数将会调用多次,允许当你移动时更新你的位置,或者提高位置的精确度。

错误的回调函数,就像getCurrentPosition()里面的一样,可以被调用多次。

注意:你不能不初始化getCurrentPosition()函数就使用watchPosition().

var watchID = navigator.geolocation.watchPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});

watchPosition()方法将会返回一个ID,你可以使用它作为已经请求的位置监听的标识符。

你可以随后使用clearWatch()方法停止监听。

navigator.geolocation.clearWatch(watchID);

Fine tuning response

getCurrentPosiitin()和watchPosition()函数都接受一个成功回调函数,一个可选的错误回调函数,一个可选的Positionoptions 对象。

function geo_success(position) {
do_something(position.coords.latitude, position.coords.longitude);
} function geo_error() {
alert("Sorry, no position available.");
} var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
}; var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);

Describing a position

使用position对象中的coords属性返回用户当前位置信息。

position.coords返回了一个coordinates对象,coordinates对象定义了当前位置信息。只读。

position.timestamp返回了一个domtimestamp,定义了多少毫秒之后重新获取位置信息。

 

coordinate对象的信息:

Coordinate.latitude:返回double数据,位置的纬度

Coordinate.longtitude:返回double数据,位置经度

Coordinate.altitude:返回相对于sea level 而言的海拔高度。

Coordinate.accuracy:返回double数据,以米为单位返回了经度和纬度的准确度。

Coordinate.altitudeAccuracy:同上,返回的是海拔高度的准确性。

Coordinate.heading:返回double数据,代表了设备当前的位置。0代表了正北。顺时针方向表示和正北位置的偏差(意味着正东是90度,正西是270度)。如果速度是0,那么heaing是NaN。如果设备不能提供heading信息,那么这个值是null、

Coordinate.speed:返回double数据,岱庙了每秒中的速度,这个值可以是null。

Handling errors

function errorCallback(error) {
alert('ERROR(' + error.code + '): ' + error.message);
};

Geolocation Live Example

HTML Content

<p><button onclick="geoFindMe()">Show my location</button></p>
<div id="out"></div>

JavaScript Content

 function geoFindMe() {
var output = document.getElementById("out"); if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
} function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude; output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>'; var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false"; output.appendChild(img);
}; function error() {
output.innerHTML = "Unable to retrieve your location";
}; output.innerHTML = "<p>Locating…</p>"; navigator.geolocation.getCurrentPosition(success, error);
}

Prompting for permission

任何附加托管在addons.mozilla.org获取地理位置的设备必须显示的同意。就像chrom上调用地理位置时候会

下面的函数将会请求同意,就像在web页面上弹出的请求promote框一样。

当同意地理位置的时候,用户的回应将会被当做pref参数传递。函数还提供了一个callback参数作为回调,此回调包含了用户选择的布尔值,当布尔值true(也就是用户同意)时,设备将会获取地理数据信息。

function prompt(window, pref, message, callback) {
let branch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch); if (branch.getPrefType(pref) === branch.PREF_STRING) {
switch (branch.getCharPref(pref)) {
case "always":
return callback(true);
case "never":
return callback(false);
}
} let done = false; function remember(value, result) {
return function() {
done = true;
branch.setCharPref(pref, value);
callback(result);
}
} let self = window.PopupNotifications.show(
window.gBrowser.selectedBrowser,
"geolocation",
message,
"geo-notification-icon",
{
label: "Share Location",
accessKey: "S",
callback: function(notification) {
done = true;
callback(true);
}
}, [
{
label: "Always Share",
accessKey: "A",
callback: remember("always", true)
},
{
label: "Never Share",
accessKey: "N",
callback: remember("never", false)
}
], {
eventCallback: function(event) {
if (event === "dismissed") {
if (!done) callback(false);
done = true;
window.PopupNotifications.remove(self);
}
},
persistWhileVisible: true
});
} prompt(window,
"extensions.foo-addon.allowGeolocation",
"Foo Add-on wants to know your location.",
function callback(allowed) { alert(allowed); });

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 5.0 3.5 (1.9.1)[1] 9 10.60
No support 15.0
16.0
5
Secure origins only 50.0        

[1] Firefox包括了支持基于你的wifi使用Google Location 服务来获取你的位置信息。在Firefox和Google连接时候,传递数据包括你WIFI获取点信息,一个access 令牌(类似于两个星期的cookie),和用户ip。

Firefox 3.6 (Gecko 1.9.2)加入了Linux系统中对于GPSD的定位支持。

使用geolocation的更多相关文章

  1. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  2. H5 Notes:Navigator Geolocation

    H5的地理位置API可以帮助我们来获取用户的地理位置,经纬度.海拔等,因此我们可以利用该API做天气应用.地图服务等. Geolocation对象是我们获取地理位置用到的对象. 首先判断浏览器是否支持 ...

  3. geolocation/ 百度地图api Geolocation 定位当前城市信息

    根据当前所处位置 定位所在城市信息 <html> <head> <meta charset="UTF-8" /> <title>js ...

  4. 完美解决window.navigator.geolocation.getCurrentPosition,在IOS10系统中无法定位问题

    目前由于许多用户都将电话升级到了iOS系统,苹果的iOS 10已经正式对外推送,相信很多用户已经更新到了最新的系统.然而,如果web站没有及时支持https协议的话,当很多用户在iOS 10下访问很多 ...

  5. Web API接口之Geolocation

    0.关于Geolocation Geolocation,地理位置API.用于获取用户的位置信息.它不算是现有的HTML5标准的“直系”成员,但是是W3C的一个标准.它几乎就是一个真正的JavaScri ...

  6. Geolocation API JavaScript访问用户的当前位置信息

    Geolocation API在浏览器中的实现是navigator.geolocation对象,常用的有以下方法. 1.第一个方法是getCurrentPosition() 调用这个方法就会触发请求用 ...

  7. JS新API标准 地理定位(navigator.geolocation)/////////zzzzzzzzzzz

    在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...

  8. JS新API标准 地理定位(navigator.geolocation)

    在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...

  9. 使用navigator.geolocation来获取用户的地理位置信息

    使用navigator.geolocation来获取用户的地理位置信息 W3C 中新添加了一个名为 Geolocation的 API 规范,Geoloaction API的作用就是通过浏览器获取用户的 ...

  10. Geolocation API 原理及方法

    使用IP地址:基于Web的数据库:无线网络连接定位:三角测量:GPS技术:来测量经度和纬度.(综合了所有技术)地理定位的精确度,有很多方法可以定位用户的地理位置,并且每种方法都有不同的精度.桌面浏览器 ...

随机推荐

  1. xamarin android 获取根证书代码

    Java.Security.KeyStore keyStore = Java.Security.KeyStore.GetInstance("AndroidCAStore"); ke ...

  2. cocos2d-x step by step(3) Double Kill

    喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈! 这就是最终界面了,没画一个球形  就用一个白色方框代替吧. 啊哈! public ...

  3. SharpSSH

    SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...

  4. Linux Distribution

    来自为知笔记(Wiz)

  5. [Algorithms] Refactor a Loop in JavaScript to Use Recursion

    Recursion is when a function calls itself. This self calling function handles at least two cases, th ...

  6. project管理之makefile与自己主动创建makefile文件过程

    (风雪之隅 http://www.laruence.com/2009/11/18/1154.html) Linux Makefile自己主动编译和链接使用的环境 想知道到Linux Makefile系 ...

  7. JavaScript技巧手冊

    js小技巧 每一项都是js中的小技巧,但十分的有用! 1.document.write(""); 输出语句  2.JS中的凝视为//  3.传统的HTML文档顺序是:documen ...

  8. JD笔试试题(凭记忆写的+人生感悟 try finally )

    京东笔试:技术篇(一套卷.包含測试.算法,研发) 一:填空题(4分 * 15) 15 个 涉及的面很广的选择题,可是比較側重基础.包含数据结构的.c++类的,操作系统的,计算机网络的. 二:编程题(2 ...

  9. Linux退出时出现there are stopped jobs如何解决?

    Linux 使用exit时出现there are stopped jobs如何解决? 这是因为一些命令被挂起了, 在后台驻留,需要关闭. 解决问题: 输入命令jobs -l显示停止进程的详细列表 可以 ...

  10. Push flow

    自动移库规则push flow可以用来规划物流 比如产品A如果进入到picking区,按照仓储的规则,系统可以自动生产调拨单,将产品A 从picking区调拨到保存的库位货架A1E1     设置步骤 ...