Firefox Geolocation API替代方案

Che*_*ipe 6 firefox geolocation

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

我想要做的是在我自己的服务器上设置一个返回特定地理位置的页面 ; 我会将lat,lon和precision值的硬代码返回到我的页面,该页面进行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"'
 .     '}'
 .    '}'
 .   '}';
Run Code Online (Sandbox Code Playgroud)

在我的调用页面上,我有以下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;
 }
}
Run Code Online (Sandbox Code Playgroud)

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

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

任何有关这方面的帮助将不胜感激.

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

Fem*_*emi 0

有趣的。我做了一些地理定位工作,并没有意识到这个小细节。盯着地理定位提供程序的源代码并借助一点 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>"}
Run Code Online (Sandbox Code Playgroud)

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