Add to Technorati Favorites The NOAA WebService
..................................
the blog@spencerwilliams.net

The NOAA WebService

January 15, 2008 14:06 by Spencer

I think something cool to add to a map is weather.  Take weather.com's use of Virtual Earth, I like it much more than the old Java timeline player.  I was looking at something done by Peter Bromberg on eggheadcafe.com.  He made some additions to a ASCX User Control that Mikhail Arkhipov created shown on his blog at MSDN.  I liked it.  I wanted it.  I downloaded Peter's project and built it.  It was pretty neat.  After integrating it into my TrailMaps website, I then realized what I didn't like about it - it was a server control.  Virtual Earth works on AJAX calls, so this didn't work for my TrailMaps website.  Or did it?  I needed to call the NOAA webservice after the map had loaded and I had a location.  Peter Bromberg's code translated zip codes into Lat/Long combinations to make it easier to implement, but I already had Lat/Long coordinates, so I reworked some of the code to query on LatLong instead of zipcode.  There was still the problem of the code needing to postback after I grabbed the LatLong from the map.  I had two choices:

1.  Write the code on the client-side to create the control via javascript that I already had on the server-side in C#. OR

2.  Figure out a way to return the ASCX control via javascript webservice calls using the AJAX Toolkit in VS 2008

I went with option 2 because I'm lazy, I liked what the control looked like already, and it's just HTML in the end right?  There has to be a way to get the HTML out of a user control and then grab that via a JS webservice call right?

 There is.  You have to use the System.Web.UI.Page class, but you can get it....you just have to render it first as if you were adding a control to a page.

public static string getWeatherControlHTML(decimal myLat, decimal myLon)
        {
            WeatherControl.WeatherLookup lookup = new WeatherControl.WeatherLookup();
            lookup.MyLat = myLat;
            lookup.MyLon = myLon;

            Page pageHolder = new Page();

            pageHolder.Controls.Add(lookup);

            StringWriter lookupOutput = new StringWriter();
            HttpContext.Current.Server.Execute(pageHolder, lookupOutput, false);

            return lookupOutput.ToString();
        }

Call this function from your webservice webmethod and you've got it.  I also added some animated gif's to simulate "working".  Check it out here and click the Get Weather button to grab the weather.  You can pan around and get weather all over the map, Colorado, New York, wherever the map is centered, those are the coordinates that are sent to the NOAA webservice.  I will warn you, Canadian weather is not supported. 

You can read more about the NOAA webservice:

Overview of the forcast webservice

 


Related posts

Comments are closed
 
Blog Information Profile for williasp