FF中地理位置的默认源是在about:config
中从参数geo.wifi.uri设置为https://www.google.com/loc/json
值。当在Javascript中进行标准的地理定位调用时,例如navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);火狐将使用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
时,我会像预期的那样拿回我的lat、lon和准确性。当我将geo.wifi.uri更改为http://mysite/json
或http://mysite.json/index.php
时,没有明显的响应,包括没有执行javascript的错误部分。
我猜想答案在地理定位-API中,但当我真正想要返回格式正确的信息时,我无法理解它。
如对此有任何帮助,将不胜感激。
TL;DR:为geo.wifi.uri生成一个有效的响应作为https://www.google.com/loc/json
的替代方案需要什么?
发布于 2011-05-04 05:39:56
有意思的。我做了一些地理定位工作,却不知道这个小细节。盯着地理位置提供程序的源代码,在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>"}
试着让你的反应看起来像那样,看看会发生什么。
发布于 2011-07-26 04:39:20
检查http://code.google.com/p/gears/wiki/GeolocationAPI“网络位置提供程序协议”部分包含请求和响应格式。
https://stackoverflow.com/questions/5878147
复制相似问题