Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Using ws.geonames.org timezone webservice without WSDL

, 4 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use ws.geonames.org/timezone and other raw webservices from windows mobile dotnet compact framework

The great site geonames.org offers some webservices. One of these is called timezone and will give you the timezone information for a given geographic Latitude and Longitude. With a GPS enabled Windows Mobile device you can so query the webservice and get timezone informations for the current location.

Unfortunately the webservice does not offer WSDL and so you have to write your own wrapper class. I wrote a small class that does the HttpWebRequest and decodes the xml reponse for easy use in your application.

When you query the webservice you have to use the following form:

<a href="http://ws.geonames.org/timezone?lat=47.01&lng=10.2">http://ws.geonames.org/timezone?lat=47.01&lng=10.2</a>

The answer will then be:

         <geonames>
            <timezone>
                <countryCode>AT</countryCode>
                <countryName>Austria</countryName>
                <lat>47.01</lat>
                <lng>10.2</lng>
                <timezoneId>Europe/Vienna</timezoneId>
                <dstOffset>2.0</dstOffset>
                <gmtOffset>1.0</gmtOffset>
                <rawOffset>1.0</rawOffset>
                <time>2010-03-02 12:14</time>
            </timezone>
        </geonames>

The sample application will use this answer and shows the result:

The attached Visual Studio 2005 SmartDevice Windows Mobile 5 project has two classes: GeonamesTZ is a blocking class and geonamesTZ is a non-blocking class.

A blocking code is never good and so I implemented the class with an event. To use the class include a using statement for the geonames namespace and then initialize a new geonamesTZ object. Then add an event handler for the object’s event delegate. Finally add a event handler function that will get the timezone data.

<span style="color: #0600FF;">using</span> <span style="color: #008080;">geonames</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Form1 <span style="color: #008000;">:</span> Form
    <span style="color: #000000;">{</span>
        geonamesTZ myGeoTZ<span style="color: #008000;">;</span>
        geonamesTZfields tzFields<span style="color: #008000;">;</span>
 
        <span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            InitializeComponent<span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            myGeoTZ <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> geonamesTZ<span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            myGeoTZ.<span style="color: #0000FF;">geonamesEventHandler</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> geonamesEvent<span style="color: #000000;">(</span>myGeoTZ_geonamesEvent<span style="color: #000000;">)</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">}</span>
 
        <span style="color: #0600FF;">void</span> myGeoTZ_geonamesEvent<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> sender, geonamesEventArgs e<span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            <span style="color: #008080; font-style: italic;">//Cursor.Current = Cursors.Default;</span>
            <span style="color: #008080; font-style: italic;">//MessageBox.Show(e.m_myEventArgumentdata.strCountryName);</span>
            tzFields <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">m_myEventArgumentdata</span><span style="color: #008000;">;</span>
            updateUI<span style="color: #000000;">(</span>tzFields<span style="color: #000000;">)</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">}</span>

To get the timezone information you have to call the class function

            myGeoTZ.<span style="color: #0000FF;">startRequest</span><span style="color: #000000;">(</span>txtLat.<span style="color: #0000FF;">Text</span>, txtLng.<span style="color: #0000FF;">Text</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//the answer should come...</span>

and after some time the function myGeoTZ_geonamesEvent is called with the received data. Using the event driven approach will give the possibility to write an app that reads GPS data and updates timezone information in the background. Maybe this is usefull for Windows Mobile phones that often ’fly’ between continents. The timezone can then be updated in background.

As updating the UI from a different thread may fail, the updateUI function is implemented like this:

        <span style="color: #008080; font-style: italic;">// see http://blog.opennetcf.com/ctacke/2008/12/03/ControlInvokeWithoutExplicitDelegates.aspx</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> updateUI<span style="color: #000000;">(</span>geonames.<span style="color: #0000FF;">geonamesTZfields</span> myTZinfos<span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">InvokeRequired</span><span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">(</span><span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> o, EventArgs a<span style="color: #000000;">)</span>
                <span style="color: #000000;">{</span>
                    txtDSToffset.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">dstOffset</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                    txtGMToffset.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">gmtOffset</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                    txtTimzoneID.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strTimezoneID</span><span style="color: #008000;">;</span>
                    txtTime.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">tzTime</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                    txtTZcountryCode.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strCountryCode</span><span style="color: #008000;">;</span>
                    lblStatus.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strLastError</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">}</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">}</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">{</span>
                txtDSToffset.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">dstOffset</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                txtGMToffset.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">gmtOffset</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                txtTimzoneID.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strTimezoneID</span><span style="color: #008000;">;</span>
                txtTime.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">tzTime</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                txtTZcountryCode.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strCountryCode</span><span style="color: #008000;">;</span>
                lblStatus.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> myTZinfos.<span style="color: #0000FF;">strLastError</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">}</span>
        <span style="color: #000000;">}</span>

That was much easier than doing invokes on every txt field separately.

The raw xml response is parsed by another class called xml_helper. This class makes it easy to extract single values from the raw xml response. Here is one example:

<span style="color: #0600FF;">namespace</span> xml_helper<span style="color: #000000;">{</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> xml_helper
    <span style="color: #000000;">{</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> getStrSetting<span style="color: #000000;">(</span>StringBuilder sb, <span style="color: #FF0000;">String</span> sField<span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            <span style="color: #FF0000;">string</span> sVal <span style="color: #008000;">=</span> sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> sFieldString <span style="color: #008000;">=</span> <span style="color: #666666;">"&lt;"</span> <span style="color: #008000;">+</span> sField <span style="color: #008000;">+</span> <span style="color: #666666;">"&gt;"</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> iIdx <span style="color: #008000;">=</span> sVal.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">(</span>sFieldString<span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> sResult <span style="color: #008000;">=</span> <span style="color: #666666;">""</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>iIdx <span style="color: #008000;">&</span>gt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #FF0000;">string</span> fieldStr <span style="color: #008000;">=</span> sVal.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">(</span>iIdx<span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span> ssidStr <span style="color: #008000;">=</span> fieldStr.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">(</span><span style="color: #FF0000;">0</span>, sFieldString.<span style="color: #0000FF;">Length</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                iIdx <span style="color: #008000;">=</span> ssidStr.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">(</span><span style="color: #666666;">"&lt;/"</span><span style="color: #000000;">)</span><span style="color: #008000;">;</span>
                sResult <span style="color: #008000;">=</span> ssidStr.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">(</span><span style="color: #FF0000;">0</span>, iIdx<span style="color: #000000;">)</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">}</span>
            <span style="color: #0600FF;">return</span> sResult<span style="color: #008000;">;</span>
        <span style="color: #000000;">}</span>

These simple functions can be easily used on a xml file with single element data. Just call, for example getStrSettings with the xml string and the setting you would like to get.

Download Visual Studio 2005 SmartDevice Windows Mobile 5 project: DOWNLOAD:TimeZone by GPS demo application -

<!-- Social Bookmarks BEGIN --> <!-- Social Bookmarks END -->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hjgode

Germany Germany
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 4 Mar 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid