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:
The answer will then be:
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>
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;">
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;"><span style="color: #0600FF;">public</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>
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 -->