Getting Geo-location of User in Silverlight

So as I promised, we are now up to getting the Geo-location of the user of our application. Believe me you will be needing this one day, when you will find that your ex is spying on you and your applications, as mine does, I suppose. It all started when she asked me one day, to choose between her and .Net; well sometimes, I really miss her. Let's not talk about my ex more here (I may be writing an entire article on her someday). Let's instead proceed to something useful.
JavaScript, yes it is, the core of our scenario. We will be calling following JavaScript API here in the aspx page:

   1: <script language="JavaScript" type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

Now there are functions in that API that will return the Latitude, Longitude, country name and whatever we demand. Some of the function are viz:

  • geoip_country_code()
  • geoip_country_name()
  • geoip_latitude()
  • geoip_longitude()

Instead of just calling these functions directly in Silverlight, we will write our functions that will call these functions and return whatever these functions return. For example here is my custom script written just beneath our API call:

   1: <script type="text/javascript">
   2:  
   3: function GetCountryCode() {
   4: return geoip_country_code();
   5:         }
   6: function GetCountryName() {
   7: return geoip_country_name();
   8:         }        
   9: function GetLatitude() {
  10: return geoip_latitude();
  11:         }
  12: function GetLongitude() {
  13: return geoip_longitude();
  14:         }
  15:  
  16: </script>

So finally here is what my aspx page with the API call and the scripts that I've written looks like.

   1: <script type="text/javascript" src="Silverlight.js"></script>
   2: <script language="JavaScript" type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
   3: <script type="text/javascript">
   4:  
   5: function GetCountryCode() {
   6: return geoip_country_code();
   7:     }
   8: function GetCountryName() {
   9: return geoip_country_name();
  10:     }
  11: function GetLatitude() {
  12: return geoip_latitude();
  13:     }
  14: function GetLongitude() {
  15: return geoip_longitude();
  16:     }    
  17: 
  18: </script>

Now let's move toward our mainpage. We must make the JavaScript function call from the main page. As you all know, it is a simple method call:

   1: HtmlPage.Window.Invoke("<JavaScriptMethod>");

The XAML of the main page consists of four Text Blocks, each to pursue the respective value for Country code, Country Name, Latitude and Longitude.

   1: <Grid x:Name="LayoutRoot" Background="White">
   2: <TextBlock x:Name="txtCountryCode" Margin="20 20 0 0"/>
   3: <TextBlock x:Name="txtCountryName" Margin="20 40 0 0"/>
   4: <TextBlock x:Name="txtLatitude" Margin="20 60 0 0"/>
   5: <TextBlock x:Name="txtLongitude" Margin="20 80 0 0"/>
   6: </Grid>

For giving the corresponding values to the Blocks we need to write some C# in the code behind, and yes in the constructor of the main page.

   1: public MainPage()
   2: {
   3:     InitializeComponent();
   4:     txtCountryCode.Text = HtmlPage.Window.Invoke("GetCountryCode").ToString();
   5:     txtCountryName.Text = HtmlPage.Window.Invoke("GetCountryName").ToString();
   6:     txtLatitude.Text = HtmlPage.Window.Invoke("GetLatitude").ToString();
   7:     txtLongitude.Text = HtmlPage.Window.Invoke("GetLongitude").ToString();   
   8: }

That's it. Just hit F5 and run the project. It will provide you all the information you've requested in the code.

You can download the sample project from: http://www.4shared.com/rar/JOZ97_5k/Geolocation.html

Stay tuned for more exciting articles…Happy Reading!!!


Similar Articles