This tutorial/code sample is a quick guide to using google latitude’s new api and a weather service to get your current weather conditions in PHP.
First off I should say that this tutorial will be using the Google Latitude API and a web service from http://www.geonames.org/ using PHP 5. This is not an end-all tutorial on how to do this, just a simple example. Using this technique you can do all sorts of stuff with your GPS position.
You’ll need to have a Google Latitude Account and probably a GPS and Web enabled Cell phone would be helpful.
Google Latitude is a service that allows you to share your current location with friends. You location is updated whenever you use google maps on your mobile device and can continue sharing your location even after you’ve exited the maps. I personally think this is pretty cool because that means I don’t have to write my own location sharing app for windows mobile. You can also update your location online via an iGoogle Widget so you don’t have to worry about crazy stalkers finding you.
So, here’s the code:
<?php
$googleLatID=”XXXXXXXXXXXXXXXXXXX”; //find this at http://www.google.com/latitude/apps/badge
$url =”http://www.google.com/latitude/apps/badge/api?user=”.$googleLatID.”&type=json”;
try{
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents, true);
$loc=”lat=”.$results['features'][0]['geometry']['coordinates'][1].”&lng=”.$results['features'][0]['geometry']['coordinates'][0];
$Wurl =”http://ws.geonames.org/findNearByWeatherJSON?”.$loc;
try{
$Wcontents=file_get_contents($Wurl);
$Wcontents=utf8_encode($Wcontents);
$Weather = json_decode($Wcontents, true);
print_r($Weather);
}
catch (Exception $e){
echo ‘WEATHER NOT FOUND!
‘;
}
}
catch (Exception $e){
echo ‘LOCATION NOT FOUND!
‘;
}
?>
Let’s break it down and see what it does:
$googleLatID=”XXXXXXXXXXXXXXXXXXX”; //find this at http://www.google.com/latitude/apps/badge
$url =”http://www.google.com/latitude/apps/badge/api?user=”.$googleLatID.”&type=json“;
The first two lines just set variables for your Google Latitude Id and the url to the web service. You will need to replace the “XXXXXXX” with your own Id, but that should be the only thing you have to change to make this work.
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents, true);
$loc=”lat=”.$results['features'][0]['geometry']['coordinates'][1].”&lng=”.$results['features'][0]['geometry']['coordinates'][0];
$Wurl =”http://ws.geonames.org/findNearByWeatherJSON?”.$loc;
This reads the contents of the JSON feed from Google then decodes it into a nice neat PHP Associative Array using json_decode(). Then we set the $loc string for your query string variables of latitude and longitude. You might notice that the lat/lon returned by Google are actually in reverse so its ['coordinates'][1], ['coordinates'][0]. $Wurl is the next url you need for the weather service.
$Wcontents=file_get_contents($Wurl);
$Wcontents=utf8_encode($Wcontents);
$Weather = json_decode($Wcontents, true);
print_r($Weather);
This does the same thing that we did above but instead of reading the feed from google you get the feed from geonames.com.
And that’s it. You now have a nice Associative Array in PHP to display however you want.
So here’s an example of doing something with google latitude, weather underground feeds and php dynamic sky image: http://dev.matthewroy.com/weather/
I hope you enjoyed this quick tutorial/code sample. I’d love to hear what you do with this so feel free to send me email or comment below.



Leave a Reply