【发布时间】: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/json 或 http://mysite.json/index.php 时,我没有得到明显的响应,包括没有执行 javascript 的错误部分。
我认为答案在 geolocation-API 中,但当我真正想要的是返回格式正确的信息时,我无法理解它。
我们将不胜感激。
TL;DR:为 geo.wifi.uri 生成有效响应以替代 https://www.google.com/loc/json 需要什么?
【问题讨论】:
标签: firefox geolocation