Firefox 地理定位 API 替代方案

发布于 2024-11-05 08:59:39 字数 2337 浏览 0 评论 0原文

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 并返回我的大致位置。

我想要做的是在我自己的服务器上设置一个返回特定地理位置的页面;我将硬编码纬度、经度和精度值,以返回到进行 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 的替代方案需要什么?

The default source for geolocation in FF is set in about:config from the parameter geo.wifi.uri to the value of https://www.google.com/loc/json. When the standard geolocation call is made in Javascript, e.g., navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors); Firefox will query https://www.google.com/loc/json with my IP address (desktop) and return my approximate location.

What I want to do is to set up a page on my own server that returns a specific geolocation; I'll hard code in the lat, lon, and accuracy value to be returned to my page that makes a getCurrentPosition call. This will let me test code for a client in various locations without actually having to go to those physical locations for testing.

Here is my problem: what format can I make my page so that it returns a valid structure / object that FF can use.

The page is hosted on Apache and will be generated from PHP.

For this example, let's say that my pseudo geolocation page is at http://mysite/json/index.php.

I tried it with the following PHP code:

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

On my calling page I have the following 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;
 }
}

With geo.wifi.uri set to https://www.google.com/loc/json I get back my lat, lon, and accuracy as expected. When I change geo.wifi.uri to http://mysite/json or to http://mysite.json/index.php I get no apparent response, including no execution of the error section of the javascript.

I presume that the answer is in the geolocation-API but I can't make sense of it when all I really want is to return properly formatted information.

Any help on this will be appreciated.

TL;DR: what is required to generate a valid response for geo.wifi.uri as an alternative to https://www.google.com/loc/json?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-11-12 08:59:39

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

尝试使您的响应看起来像这样,看看会发生什么。

Interesting. I've done a little geolocation work, and wasn't aware of this little detail. Staring at the source for the geolocation provider and with a little bit of PHP help, you get this as Google's response:

{"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>"}

Try making your response look like that and see what happens.

岛徒 2024-11-12 08:59:39

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

Check http://code.google.com/p/gears/wiki/GeolocationAPI the section "Network Location Provider Protocol" contains both request and response format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文