Implement Yahoo weather API along with Google map integration

May 2nd, 2010 by Usha Informatique Team Leave a reply »


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


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


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

$doc = new DOMDocument();


//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;





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

Simply enter the city and from the url retrieve the value for e.g in, 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=””></script>
<script type=”text/javascript”>
google.load(”maps”, “2.x”);
<script type=”text/javascript” charset=”utf-8″>
var map = new GMap2($(”#mapdiv”).get(0));

var delhi = new GLatLng(<?php echo $latitude; ?>,<?php echo $longitude; ?>);
marker = new GMarker(delhi);
<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;}

<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



  1. Thanks for this great post – I will be sure to check out your blog more often

  2. I like that you

    think. Thank you for share very

  3. thanks for great informations It’s a wonderful

  4. It’s good to see this information in your post, i was looking the same but there was not any proper resource, thanx now i have the link which i was looking for my research. . . . . .