Archive for the ‘Google’ category

Implement Yahoo weather API along with Google map integration

May 2nd, 2010

Requirements

We got a requirement to display weather for a city along with google map for the city.

Approach

There can be two approach that can be used

1) Implement yahoo weather API and Google map separately thus each is working independently

2) Implement google map using the information derived from yahoo weather API

Here i am going to talk about the second approach

To access weather for any city using Yahoo weather API, you have to use the following code

<?php

<span>XML</span> document into memory first.

$doc = new DOMDocument();

$doc->load(’http://weather.yahooapis.com/forecastrss?w=’.$weatherlocation.’&u=c‘);

//now I get all elements inside this document with the following name “channel”, this is the ‘root’

$channel = $doc->getElementsByTagName(”channel”);

//now I go through each item withing $channel

foreach($channel as $chnl)
{

$item = $chnl->getElementsByTagName(”item”);

foreach($item as $itemgotten)
{

echo “<b>”.$itemgotten->getElementsByTagName(”title”)->item(0)->nodeValue.”</b><br/>”;

//now I search within ‘$item’ for the element “description”

$describe = $itemgotten->getElementsByTagName(”description”);

//once I find it I create a variable named “$description” and assign the value of the Element to it

$description = $describe->item(0)->nodeValue;

//and display it on-screen

echo $description;

$latitude=$itemgotten->getElementsByTagName(”lat”)->item(0)->nodeValue;

$longitude=$itemgotten->getElementsByTagName(”long”)->item(0)->nodeValue;

}

}
?>

In the above code, the yahoo weather url is called with arguments w and u (Unit of temperature). “w” is the location of the city you want the get the weather for. This you will get from yahoo weather page http://weather.yahoo.com.

Simply enter the city and from the url retrieve the value for e.g in http://weather.yahoo.com/united-states/california/bombay-2366506/, 2366506 is the value of “w”.

The last two lines in the code above are the tricky one’s. From the last two lines, you can retrieve the latitude and longitude of the place, thus think you are saving so much database space by not storing latitude and longitude of places.

After getting the latitude and longitude for the place just pass to it google map API code which in my case is like the following

<script type=”text/javascript” src=”http://www.google.com/jsapi?key=ABQIAAAAQg2VLDfgi8yxVrFlqjXF7xTQHiNlpCS1vy260As8BGMS2rl5kBTvFuKiyHlh1z2mfCbd6sJqj9WfIw”></script>
<script type=”text/javascript”>
google.load(”maps”, “2.x”);
</script>
<script type=”text/javascript” charset=”utf-8″>
$(document).ready(function(){
var map = new GMap2($(”#mapdiv”).get(0));

var delhi = new GLatLng(<?php echo $latitude; ?>,<?php echo $longitude; ?>);
map.setCenter(delhi,8);
marker = new GMarker(delhi);
map.addOverlay(marker);
});
</script>
<style type=”text/css” media=”screen”>
#mapdiv { float:left; width:213px; height:144px;padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;overflow:hidden;border:1px solid #CCCCCC;}

</style>
<div id=”mapdiv”></div>

Thus i would have both weather and google map for the place by just storing one value in the database¬† i.e. “w” factor.

Thanks !! Ushainformatique Development Team