MapQuest Developer Blog

Click the Map for Weather

It's summer time and no matter where you are, that can mean some pretty interesting weather! Let's explore what we can do to mash-up weather forecasts with MapQuest maps. Step One:  Find a simple weather web service. I like to use the GeoNames web service for two reasons: it's free and in json. Further documentation for the GeoNames weather web service can be found here http://www.geonames.org/export/JSON-webservices.html. Click map for weather Step Two:  Get a map from the map constructor (http://www.mapquestapi.com/sdk/js/v6.0.0/basic-map.html#basicmap). and create a location. To do this, add a click event listener to the map (http://www.mapquestapi.com/sdk/js/v6.0.0/basic-map.html#events). Step Three:  Now grab the lat/lng from the map click event and use it in the GeoNames service request.  You can use your own username! Once you have the weather json response, you can grab the current conditions. I created a simple POI (point of interest) with the station name in the title, and the clouds and temperature in the content window. After adding the POI to the map, the info window is toggled on since it's the only POI on the map. Congratulations: You now have weather on click anywhere in the US! That's all there is to it! You can see it working here http://www.mqdemo.com/brian/clickEvent.html.