使用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技术:来测量经度和纬度.(综合了所有技术)地理定位的精确度,有很多方法可以定位用户的地理位置,并且每种方法都有不同的精度.桌面浏览器 ...
随机推荐
- ubuntu navicat for mysql破解
ubuntu navicat for mysql破解 ubuntu navicat for mysql只能试用14天. 破解方法:rm -rf /home/cxg/.navicat64/
- vue Syntax Error: Unexpected token {
> music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...
- [c++菜鸟]《Accelerate C++》读书笔记
第0章 开始学习C++ 1.<<的行为取决于它的操作数类型,<<会把它的右操作数的字符写到左操作数所指示的流中,他是结果就是它的左操作数. 2.std::endl是一个控制器, ...
- Git 常用场景操作
git init 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹. git clone 获取一个u ...
- 关于Adapter对数据库的查询、删除操作
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- JSP技术基础(动态网页基础)
前言:如果说html为静态网页基础,那么jsp就是动态网页基础,两者的区别就是jsp在html的前面多加了几行而已.当然,jsp里面对java的支持度更高.要明白,js只是嵌入在客户端的小程序小脚本而 ...
- c# vitural
virtual关键字用于指定属性或方法在派生类中重写. 默认情况下,派生类类从其基类继承属性和方法,如果继承的属性或方法需要在派生类中有不同的行为,则可以重写它,即可以在派生类中定义该属性或方法的新实 ...
- 《UNIX-Shell编程24学时教程》读书笔记chap7 变量
7.0 本章内容: 定义,访问,删除标题和数组变量:环境变量和shell变量 7.1 定义变量 标量一次只存储一个值[名字值对]:数组变量可以存储多个值. 以数字开头的变量名如1,2或11将保留为Sh ...
- python(24)- 面向对象进阶
面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...
- VueJS样式绑定:v-bind
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...