【问题标题】:Firefox Geolocation API AlternativeFirefox 地理定位 API 替代方案
【发布时间】:2011-08-18 04:49:18
【问题描述】:

FF 中地理位置的默认来源在about:config 中设置,从参数geo.wifi.uri 到https://www.google.com/loc/json 的值。当使用 Javascript 进行标准地理定位调用时,例如 navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors); Firefox 将使用我的 IP 地址(桌面)查询https://www.google.com/loc/json 并返回我的大概位置。

我要做的是在我自己的服务器上设置一个返回特定地理位置的页面;我将在 lat、lon 和准确度值中进行硬编码,以返回到进行 getCurrentPosition 调用的页面。这将让我在不同位置为客户测试代码,而无需实际前往这些物理位置进行测试。

这是我的问题:我可以制作什么格式的页面,以便它返回 FF 可以使用的有效结构/对象。

页面托管在 Apache 上,将由 PHP 生成。

对于这个例子,假设我的伪地理位置页面位于http://mysite/json/index.php

我用下面的 PHP 代码试了一下:

header('Content-type: application/json');
echo '{"position":'
 .    '{"coords":'
 .     '{'
 .      '"latitude":"80",'
 .      '"longitude":"-20",'
 .      '"altitude":"299",'
 .      '"accuracy":"111",'
 .      '"altitudeAccuracy":"222",'
 .      '"heading":"333",'
 .      '"speed":"44"'
 .     '}'
 .    '}'
 .   '}';

在我的调用页面上,我有以下 javascript:

function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
}

function handle_geolocation_query(position){
 var foo = 'Lat: ' + position.coords.latitude + "\n"
         + 'Lon: ' + position.coords.longitude + "\n"
         + 'Alt: ' + position.coords.altitude + "\n"
         + 'Acc: ' + position.coords.accuracy + "\n"
         + 'AAc: ' + position.coords.altitudeAccuracy + "\n"
         ;
 alert(foo);
}

function handle_errors(error) {
 switch(error.code) {
  case error.PERMISSION_DENIED: alert("user did not share geolocation data");
  break;

  case error.POSITION_UNAVAILABLE: alert("could not detect current position");
  break;

  case error.TIMEOUT: alert("retrieving position timed out");
  break;

  default: alert("unknown error");
  break;
 }
}

将 geo.wifi.uri 设置为 https://www.google.com/loc/json 后,我可以按预期恢复我的纬度、经度和准确度。当我将 geo.wifi.uri 更改为 http://mysite/jsonhttp://mysite.json/index.php 时,我没有得到明显的响应,包括没有执行 javascript 的错误部分。

我认为答案在 geolocation-API 中,但当我真正想要的是返回格式正确的信息时,我无法理解它。

我们将不胜感激。

TL;DR:为 geo.wifi.uri 生成有效响应以替代 https://www.google.com/loc/json 需要什么?

【问题讨论】:

    标签: firefox geolocation


    【解决方案1】:

    有趣。我做了一些地理定位工作,并没有意识到这个小细节。查看地理定位提供程序的源代码并借助一点 PHP 帮助,您会得到 Google 的回复:

    {"location":{
        "latitude":<latitude value>,
        "longitude":<longitude value>,
        "address":{
        "country":"United States",
        "country_code":"US",
        "region":"<state>", 
        "city":"<city>",
        "street":"<streetname>",
        "street_number":"<street number>",
        "postal_code":"<zip code>" 
        }, 
        "accuracy":30.0} ,
     "access_token":"<long access token string>"}
    

    试着让你的回复看起来像这样,看看会发生什么。

    【讨论】:

      【解决方案2】:

      检查http://code.google.com/p/gears/wiki/GeolocationAPI“网络位置提供程序协议”部分包含请求和响应格式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        • 2015-12-15
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多