使用带有纬度和经度的 Google Weather API - 如何格式化?

发布于 2024-08-31 04:52:22 字数 462 浏览 2 评论 0原文

我想使用 Google Weather API - 通过传递纬度和经度值。我正在存储这些值,但是 Google 似乎需要不同格式的这些值。

即对于 McTavish 镇,我的值为 45.5 和 -73.583

这在这里工作: http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=45.5,-73.583

但是当我将这些数据用于 Google API 时,它不起作用:请参阅:www.google.com/ig/api?weather=,,,45.5,-73.583

感谢任何帮助。我更喜欢使用谷歌数据。

I want to use the Google Weather API - by passing lat and long values. I am storing these values, however it seems Google needs these values in different format.

i.e. For the town of McTavish I have values of 45.5 and -73.583

This works here: http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=45.5,-73.583

But when I use these data for Google API, it does not work: See: www.google.com/ig/api?weather=,,,45.5,-73.583

Any help appreciated. I would prefer to use the Google Data.

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

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

发布评论

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

评论(4

如痴如狂 2024-09-07 04:52:22

更新的答案:
我刚刚注意到 Google 天气 API 存在其他一些违规行为。在任何情况下,除了负号(如果适用)之外,您还需要有 8 位数字。请参阅以下代码块(基于 Java)以了解正确的格式。 (不是完美的算法,只是一个简单的例子,以便您可以看到结果)

lat = lat.replace(".", "");
while(lat.length() < 9)
    lat = lat.concat("0");
if(lat.contains("-"))
    lat = lat.substring(0, 9);
else
    lat = lat.substring(0, 8);

原始响应:
Paul,Google 天气 API 的窍门在于您不使用传统纬度/经度接收的坐标。相反,您解析出小数点。此外,Google 天气 API 的一个“有趣的怪癖”似乎是要求数据以 7 到 8 位字符串的形式出现。因此,例如,45.5 实际上应该是 45.50000,而 -73.583 实际上应该是 -73.58300。这个 7-8 位数字的长度似乎不包含任何负坐标前面的负号 (-)。

因此,您的 45.5(0000) 变为 4550000,您的 -73.583(00) 变为 -7358300。因此最终的 URL 将是:

http://www.google.com/ig/api?weather=,,,4550000,-7358300

请再次注意,7-8 位数字表示 455000045500000 是可以接受的,-7358300 或 <代码>-73583000。

当我看到你的问题时,我才发现7-8位数字的长度——我尝试将数据输入到我的天气解析程序中,发现455,-73583不能产生正确的数据。

请注意,这是我的非官方实验,而不是官方文档,因此可能还会发现其他怪癖。

UPDATED ANSWER:
I have just noticed some OTHER irregularities with Google's Weather API. In ANY case, you need to have 8 numerical digits, in addition to the negative sign, if it applies. See the following code block (Java-based) for proper formatting. (Not the perfect algorithm, but just a quick example so that you can see the results)

lat = lat.replace(".", "");
while(lat.length() < 9)
    lat = lat.concat("0");
if(lat.contains("-"))
    lat = lat.substring(0, 9);
else
    lat = lat.substring(0, 8);

ORIGINAL RESPONSE:
Paul, the trick about Google's Weather API is that you don't use the coordinates as received by traditional latitude/longitude. Instead, you parse out the decimal points. Additionally, a "fun quirk" of Google's Weather API seems to be a requirement that the data come in as a 7- to 8-digit string. So, for instance, 45.5 should really be 45.50000, and -73.583 should really be -73.58300. This length of 7-8 digits does NOT seem to include the negative sign (-) in front of any negative coordinates.

So, your 45.5(0000) becomes 4550000, and your -73.583(00) becomes -7358300. So the final URL would be:

http://www.google.com/ig/api?weather=,,,4550000,-7358300

Note that again, 7-8 digits means 4550000 or 45500000 would be acceptable, as would -7358300 or -73583000.

I only found out about the 7-8 digit length when I saw your question--I tried entering the data into my weather parsing program, and found that 455,-73583 does not yield proper data.

Note that this is by my unofficial experimentation, and not by official documentation, so there may be other quirks to be discovered.

我做我的改变 2024-09-07 04:52:22

它更简单 - 纬度和纬度经度应乘以一百万

It's much simpler - latitude & longitude should be multiplied by one million

兰花执着 2024-09-07 04:52:22

在我看来,经度和纬度必须以 _e6 格式编码(最后六位数字必须是传递给 API 的 lon/lat 字符串的小数部分)。然后,您必须将第一个小数点的长度调整为六位,如果小数点位数少于六位,则添加 0;如果小数点位数较多,则将其剪裁为 6。对于 int 部分,如果只有一个字符,则必须先添加一个零,对于两个或三个数字,则不需要执行任何操作。

示例:

  • 1.1234 必须编码为:-01123400
  • 112.2345 必须编码为:112234500
  • 34.123456 必须编码为

这里有示例说明和 php 源代码(博客是用西班牙语写的)。

In my opinion longitude an latitude must be coded in _e6 format (six last digits must be the decimal part of the lon/lat string passed to the API). Then you must adjust first decimals to a length of six, you add 0s if you have fewer than six decimals, and you clip it to 6, if you have more. For the int part if you have just one character you must add a zero first, for two or three digits, you don't need to do anything.

Examples:

  • 1.1234 must be coded as: -01123400
  • 112.2345 must be coded as: 112234500
  • 34.123456 must be coded as

Here you have a explanation with examples and a the php source code (the blog is written in Spanish).

Oo萌小芽oO 2024-09-07 04:52:22

正如 Miguel 所说,最后 6 位数字应该是小数位。所以正确的代码很简单......

string.Format("http://www.google.com/ig/api?weather=,,,{0:0},{1:0}", 
    (latitude * 1000000), 
    (longitude * 1000000));

As Miguel said it is the last 6 digits that should be the decimal places. So the correct code is as simple as....

string.Format("http://www.google.com/ig/api?weather=,,,{0:0},{1:0}", 
    (latitude * 1000000), 
    (longitude * 1000000));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文