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
并返回我的大概位置。Firefox地理位置API替代
我想要做的是在我自己的服务器上设置一个页面,该页面返回特定的地理位置;我将在lat,lon和准确性值中对代码进行硬编码,然后将其返回给我的页面,从而进行getCurrentPosition调用。这将使我可以在不同地点为客户测试代码,而不必实际去这些物理位置进行测试。
这是我的问题:我可以使我的页面成为什么格式,以便它返回FF可以使用的有效结构/对象。
该页面托管在Apache上,并将从PHP生成。
在这个例子中,假设我的伪地理位置页面位于http://mysite/json/index.php
。
header('Content-type: application/json');
echo '{"position":'
. '{"coords":'
. '{'
. '"latitude":"80",'
. '"longitude":"-20",'
. '"altitude":"299",'
. '"accuracy":"111",'
. '"altitudeAccuracy":"222",'
. '"heading":"333",'
. '"speed":"44"'
. '}'
. '}'
. '}';
在我的调用页面,我有以下的javascript:
我用下面的PHP代码试了一下
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/json
或http://mysite.json/index.php
时,我得不到明显的回应,包括没有执行javascript的错误部分。
我认为答案是在geolocation-API,但我无法理解它,当我真正想要的是返回格式正确的信息。
任何帮助,将不胜感激。
TL; DR:作为https://www.google.com/loc/json
的替代方案,生成geo.wifi.uri的有效响应需要什么?