Using Google Weather API with Lat and Lon - how to format?

后端 未结 4 684
星月不相逢
星月不相逢 2020-12-17 02:45

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

相关标签:
4条回答
  • 2020-12-17 03:20

    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).

    0 讨论(0)
  • 2020-12-17 03:24

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

    0 讨论(0)
  • 2020-12-17 03:25

    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.

    0 讨论(0)
  • 2020-12-17 03:45

    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));
    
    0 讨论(0)
提交回复
热议问题