首页 > LBS技术 > 详解firefox的位置定位体验功能

详解firefox的位置定位体验功能

2009年12月9日 ant 发表评论 阅读评论 9,618 views

firefox在3.5版本中加入了位置定位体验功能,使用无线上网的firefox用户通过位置定位能够快速确定自己的位置。对于使用笔记本的朋友来说,这个功能很好很强大。
一.定位效果测试
在firefox下访问http://ditu.google.com 或 maps.google.com
5

点击地图左上角的小圆圈,firefox会提示是否共享方位,

2

点“共享方位信息”后,地图上就能看到你当前的位置了。

3

4

我的实际位置是在翠宫饭店,从卫星地图上看定位还是很精确的。

二.位置定位原理

firefox位置定位功能的原理:
1.web应用使用api向fierfox申请位置定位功能
2.在得到授权的前提下,由firefox负责搜集附近的无线热点信息。
3.firefox将无线热点信息提交到google,google计算出对应的位置经纬度返回给firefox
4.firefox将经纬度结果返回给web应用,web应用根据经纬度显示相应的地图。

三.开发者如何调用位置定位

firefox和google的合作是深层次的,firefox把所有的操作(wifi信息的获取,向google提交wifi信息,解析google的返回结果。。。)都已经封装好了,对我们来说,只需调用简单的几行javascript代码就可以调用位置定位功能了。

var gl = null;

function displayPosition(position) {
 p = document.getElementById("p");
 p.innerHTML = "<a href='www.anttna.com'>www.anttna.com</a><br/><table border='1'><tr><th>时间</th><td align=center>"+ position.timestamp +
 "<tr><th width=150>纬度</th><td width=300 align=center>" + position.coords.latitude + " </td></tr>" +
 "<tr><th width=150>经度</th><td width=300 align=center>" + position.coords.longitude + " </td></tr></table>";
}

function displayError(positionError) {
 alert("error")
}

try {
 if(navigator.userAgent.indexOf("Firefox")<0) {
 gl = google.gears.factory.create('beta.geolocation');
 } else {
 gl = navigator.geolocation;
 }
}catch(e){}

if (gl) {
 gl.getCurrentPosition(displayPosition, displayError);
} else {
 alert("I'm sorry, but geolocation services are not supported by your browser.");
}

示例

四.其它

最后,还有一个问题需要弄明白,firefox如何向google提交查询信息?
这个需要抓包分析,给大家推荐一个很好用的firefox插件”live http headers”,专门用来抓firefox的网络数据包。
打开ditu.google.com,等网页加载完后,打开live http headers,点击位置定位,确认共享方位信息后。看看抓到了些什么数据。

https://www.google.com/loc/json

POST /loc/json HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en,zh-cn;q=0.7,zh;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive
Content-Length: 547
Content-Type: text/plain; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache
{"version":"1.1.0","access_token":"2:kmJ45pxFnDTzuUnn:Qp3XkWdoDBawnqdq",
"wifi_towers":[
{"mac_address":"02-21-6a-04-f3-0c","ssid":"123","signal_strength":-56},
{"mac_address":"00-0c-6e-9b-c5-e5","ssid":"hbds5","signal_strength":-68},
{"mac_address":"00-1e-e5-2b-82-17","ssid":"ktwap-3","signal_strength":-46},
{"mac_address":"00-17-7b-0f-42-70","ssid":"CECT-CHINACOMM","signal_strength":-56},
{"mac_address":"00-0c-6e-9b-c6-45","ssid":"KTHW-913","signal_strength":-69},
{"mac_address":"00-17-7b-0f-46-f8","ssid":"CECT-CHINACOMM","signal_strength":-56}]}
HTTP/1.x 200 OK
Content-Type: application/json; charset=UTF-8
Content-Encoding: gzip
Date: Fri, 04 Dec 2009 09:48:46 GMT
Expires: Fri, 04 Dec 2009 09:48:46 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Content-Length: 87
Server: GFE/2.0
----------------------------------------------------------

看到“https://www.google.com/loc/json”这个url以及post的数据格式,是不是觉得很熟悉呢?哈哈,对了,这个就是我们之前提到的google GeolocationAPI,firefox也是用的这个api。

  1. 2009年12月12日10:12 | #1

    嗯。。。不过计算机上很少有附带GPS的,不然估计会把你的GPS一起上报给GOOGLE了。

  2. 2009年12月12日22:37 | #2

    你这文章很丰富,大家一定要转告转告。

  3. CIB
    2010年2月7日03:04 | #3

    计算机外接一个GPS估计就可以做到“LAIN”所说的了吧?

  1. 2010年11月12日10:46 | #1