使用geolocation
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的更多相关文章
- HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)
1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息. 但是c ...
- H5 Notes:Navigator Geolocation
H5的地理位置API可以帮助我们来获取用户的地理位置,经纬度.海拔等,因此我们可以利用该API做天气应用.地图服务等. Geolocation对象是我们获取地理位置用到的对象. 首先判断浏览器是否支持 ...
- geolocation/ 百度地图api Geolocation 定位当前城市信息
根据当前所处位置 定位所在城市信息 <html> <head> <meta charset="UTF-8" /> <title>js ...
- 完美解决window.navigator.geolocation.getCurrentPosition,在IOS10系统中无法定位问题
目前由于许多用户都将电话升级到了iOS系统,苹果的iOS 10已经正式对外推送,相信很多用户已经更新到了最新的系统.然而,如果web站没有及时支持https协议的话,当很多用户在iOS 10下访问很多 ...
- Web API接口之Geolocation
0.关于Geolocation Geolocation,地理位置API.用于获取用户的位置信息.它不算是现有的HTML5标准的“直系”成员,但是是W3C的一个标准.它几乎就是一个真正的JavaScri ...
- Geolocation API JavaScript访问用户的当前位置信息
Geolocation API在浏览器中的实现是navigator.geolocation对象,常用的有以下方法. 1.第一个方法是getCurrentPosition() 调用这个方法就会触发请求用 ...
- JS新API标准 地理定位(navigator.geolocation)/////////zzzzzzzzzzz
在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...
- JS新API标准 地理定位(navigator.geolocation)
在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...
- 使用navigator.geolocation来获取用户的地理位置信息
使用navigator.geolocation来获取用户的地理位置信息 W3C 中新添加了一个名为 Geolocation的 API 规范,Geoloaction API的作用就是通过浏览器获取用户的 ...
- Geolocation API 原理及方法
使用IP地址:基于Web的数据库:无线网络连接定位:三角测量:GPS技术:来测量经度和纬度.(综合了所有技术)地理定位的精确度,有很多方法可以定位用户的地理位置,并且每种方法都有不同的精度.桌面浏览器 ...
随机推荐
- js中多行字符串拼接
前言 我们会经常遇到这样的场景,需要拼接多行字符串,在字符串中动态插入一些数据以达到业务的需求.但是js中并没有标准的多行编辑的函数,于是聪明的程序员们便脑洞大开,书写出许多有趣的方法. 1 2 3 ...
- mac升级系统自带numpy失败解决方案
sudo pip install -U numpy 后抛出 OSError: [Errno 1] Operation not permitted: '/tmp/pip-o2xinZ-uninstall ...
- CD_Lulu软件著作权中软件分类号
计算机软件著作权 登记中使用的软件分类编码指南 一.计算机软件著作权登记中使用的软件分类编码的结构采用组合代码结构,由9位数字组成并按照从左至右的顺序排列,前5位数字代表计算机软件分的代码:后4位数字 ...
- Flash中如何使用滤镜
使用滤镜 应用或删除滤镜 复制和粘贴滤镜 为对象应用预设滤镜 启用或禁用应用于对象的滤镜 启用或禁用应用于对象的所有滤镜 创建预设滤镜库 对象每添加一个新的滤镜,在属性检查器中,就会将其添加到该对象所 ...
- 怎样在C语言里实现“面向对象编程”
有人觉得面向对象是C++/Java这样的高级语言的专利,实际不是这样.面向对象作为一种设计方法.是不限制语言的.仅仅能说,用C++/Java这样的语法来实现面向对象会更easy.更自然一些. 在本节中 ...
- mdadm
http://en.wikipedia.org/wiki/Mdadm mdadm From Wikipedia, the free encyclopedia mdadm Original au ...
- 【课程笔记】比特币和数字货币技术[Bitcoin and Cryptocurrency Technologies] week1
源地址(可能要FQ):https://www.coursera.org/learn/cryptocurrency/home/welcome 1.1 Cryptographic Hash Functio ...
- CentOS Linux搭建独立SVN Server全套流程(转)
环境为centos6.3 1.首先 看看机器上安装了svn了没有 rpm -qa |grep svn 2.如果没有安装 执行 yum -y install subversion 3.安装好了之后 新建 ...
- python--面向对象—接口
开放封闭原则依赖导致原则接口隔离原则继承多态抽象类和接口类 编程思想:为子类做规范 归一化设计:几个类都实现了相同的方法 抽象类:最好单继承,且可以简单的实现功能 接口类:可以多继承,且最好不实 ...
- angular 绑定数据时添加HTML标签被识别的问题
由于安全性,angular本身会对绑定的HTML标签属性进行转义,所以有些情况下我们需要用到绑定的数据里面传入html标签的时候, 需要用到一个服务:$sce $sce 服务下面的一个 $sce.tr ...