Latitude & longitude weather API

While looking for a free and decent weather service API to display localised weather forecasts on tudomo‘s property details pages, I came across another blog describing how to get the current weather by using the iGoogle weather gadget API.

The examples provided were mainly to locate the weather in a certain known city. My situation was a bit different as I never know beforehand where the properties for sale on tudomo are located in the world. So what I needed was a weather service based on latitude and longitude solely. Then it would not matter where in the world a property was located as I would get the closest possible matching weather measurement.

To accomplish this you need to change the original query to the following. Please note all the commas are needed to make it work.:

http://www.google.com/ig/api?weather=,,,36575560,-04670219

Which will output some XML as the following:

<xml_api_reply version="1">

<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">

<forecast_information>
<city data=""/>
<postal_code data=""/>
<latitude_e6 data="36575560"/>
<longitude_e6 data="-4670219"/>
<forecast_date data="2009-05-11"/>
<current_date_time data="2009-05-11 17:09:51 +0000"/>
<unit_system data="US"/>
</forecast_information>

<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="71"/>
<temp_c data="21"/>
<humidity data="Humidity: 62%"/>
<icon data="/ig/images/weather/partly_cloudy.png"/>
<wind_condition data="Wind: SW at 13 mph"/>
</current_conditions>

<forecast_conditions>
<day_of_week data="Mon"/>
<low data="53"/>
<high data="75"/>
<icon data="/ig/images/weather/sunny.png"/>
<condition data="Clear"/>
</forecast_conditions>

<forecast_conditions>
<day_of_week data="Tue"/>
<low data="53"/>
<high data="75"/>
<icon data="/ig/images/weather/sunny.png"/>
<condition data="Clear"/>
</forecast_conditions>

<forecast_conditions>
<day_of_week data="Wed"/>
<low data="53"/>
<high data="75"/>
<icon data="/ig/images/weather/sunny.png"/>
<condition data="Clear"/>
</forecast_conditions>

<forecast_conditions>
<day_of_week data="Thu"/>
<low data="46"/>
<high data="71"/>
<icon data="/ig/images/weather/mostly_sunny.png"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
</weather>
</xml_api_reply>

Now it is up to you to simply parse the XML and process it as you wish. Please note that this is untested as I did not end up using the iGoogle API but instead Wunderground‘s as I felt it was more dedicated for the purpose. The same example call with Wunderground’s weather API would be:

http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=36.575560,-4.670219

Note there is a slight difference on how the latitude and longitude values are included in the GET request between the two APIs.

Comments

5 responses to “Latitude & longitude weather API”

  1. cartes sorties France avatar
    cartes sorties France

    Hi,
    I agree : the google weather api looks great and very fast but is not reliable when entering lat & lng.
    did anybody find the secret on how to make it work ?

  2. afonseca avatar
    afonseca

    Looks like the XML I tried to post got stripped out, but here is what it returned for the current temp: 23F/-5C

  3. afonseca avatar
    afonseca

    I wish I could give you a better time interval to try and reproduce the results, it seems pretty random to me. I try like 4-5 times and as you say, the same result. I try a bit later (just refresh the browser) and it’s different. Here is an example of values returned for the same request you have here with lat/long:

    I’m not sure it ever gets that cold where you are, but using the other link I get the current temp to be 54F/12C which I think is the right one.

    I’d love to use Wunderground directly but I’m looking to use this in a mobile app I’m developing and going to sell on the marketplace for a couple of dollars. According to the terms of use:
    “As with all data that you access from the Site, the data that you pull from Wunderground Data Feed may only be used by you for personal, non-commercial purposes. If you want to use that data for commercial purposes, contact us through the Site and we will supply you with rate information for commercial customers.

    If you violate any of the general or specific terms applicable to Wunderground Data Feed, WUI has the right to terminate your use of the Site and the Data Feed and to take appropriate legal actions against you.”

    I’m not building a weather app, just that I’d like to include the current weather as part of my app so I was looking for something free/easy, but it has to work.

  4. mr-euro avatar

    @afonseca

    I have tried refreshing the Google API lat/long request several times but I do still get the same results every time.

    Are you sure you can not use Wunderground’s?

  5. afonseca avatar
    afonseca

    Thanks for the writeup, I was looking for something like this. Unfortunately, after playing with this for a while I figured out it’s not very reliable when using lat/long. Give this a try, compare the XML output between these two methods:

    http://www.google.com/ig/api?weather=,,,36575560,-04670219
    http://www.google.com/ig/api?weather=mijas,%20spain

    They do match at times but other times you’ll get strange values that differ greatly (just refresh your browser after a while with the lat long version to see what I mean).

    Any ideas? The Google API was the only “free” one for commercial use that I found. Looks like I’ll also have to look into a more reliable API because I want to use lat/long.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?