Friday, October 14, 2011

How to get geo-location from ip address in asp .net


What is Geo-location?

Geo-location is a feature to get the approximate location of a user visiting a website or service, through a computer or a cell phone connected to internet.
First, all we need to do is register at http://ipinfodb.com using following link and get a free API key:
We will need this API key to use their geo-location service.
The following geo-location code will then use our API key and will return the location of an IP address (country, region, city, zip code, latitude, longitude) and the associated time zone in XML format.
*Note: We will use Visual Studio Web Project for this sample.

Sample Code:

We will need following entity classes to capture the xml output:
  1. public class LocationInfo   
  2. {  
  3.     public string CountryCode { getset; }   
  4.     public string CountryName { getset; }   
  5.     public string RegionName { getset; }   
  6.     public string CityName { getset; }   
  7.     public string ZipPostalCode { getset; }  
  8.     public TimezoneInfo Timezone { getset; }  
  9.     public LatLongInfo Position { getset; }   
  10. }  
  1. public class TimezoneInfo  
  2. {  
  3.     public string TimezoneName { getset; }  
  4.     public float Gmtoffset { getset; }  
  5.     public bool Dstoffset { getset; }  
  6. }  
  1. public class LatLongInfo  
  2. {  
  3.     public float Latitude { getset; }  
  4.     public float Longitude { getset; }   
  5. }  
 Use following code in Asp .Net web page:
  1. using System;  
  2. using System.Linq;  
  3. using System.Xml.Linq;  
  4. using System.Net;  
  5.   
  6. public partial class Default : System.Web.UI.Page  
  7. {  
  8.     protected void Page_Load(object sender, EventArgs e)  
  9.     {  
  10.         string sourceIP = string.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"])   
  11.             ? Request.ServerVariables["REMOTE_ADDR"]   
  12.             : Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  13.   
  14.         var location = HostIpToLocation(sourceIP);  
  15.     }  
  16.   
  17.     public static LocationInfo HostIpToLocation(string ip)   
  18.     {  
  19.         string url = "http://api.ipinfodb.com/v2/ip_query.php?key={0}&ip={1}&timezone=true";  
  20.   
  21.         url = String.Format(url, "<API key here>", ip);  
  22.   
  23.         HttpWebRequest httpWRequest = (HttpWebRequest)WebRequest.Create(url);  
  24.         using (HttpWebResponse httpWResponse = (HttpWebResponse)httpWRequest.GetResponse())  
  25.         {  
  26.             var result = XDocument.Load(httpWResponse.GetResponseStream());  
  27.   
  28.             var location = (from x in result.Descendants("Response")  
  29.                             select new LocationInfo  
  30.                             {  
  31.                                 CountryCode = (string)x.Element("CountryCode"),  
  32.                                 CountryName = (string)x.Element("CountryName"),  
  33.                                 RegionName = (string)x.Element("RegionName"),  
  34.                                 CityName = (string)x.Element("City"),  
  35.                                 ZipPostalCode = (string)x.Element("ZipPostalCode"),  
  36.                                 Timezone = new TimezoneInfo  
  37.                                 {  
  38.                                     TimezoneName = (string)x.Element("TimezoneName"),  
  39.                                     Gmtoffset = (float)x.Element("Gmtoffset"),  
  40.                                     Dstoffset = (bool)x.Element("Dstoffset")  
  41.                                 },  
  42.                                 Position = new LatLongInfo  
  43.                                 {  
  44.                                     Latitude = (float)x.Element("Latitude"),  
  45.                                     Longitude = (float)x.Element("Longitude")  
  46.                                 }  
  47.                             }).First();  
  48.   
  49.             return location;  
  50.         }  
  51.     }  
  52. }  
Following is a sample geo-location xml output for host IP address 74.125.45.100:
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <Response>  
  3. <Status>OK</Status>   
  4. <CountryCode>US</CountryCode>   
  5. <CountryName>United States</CountryName>   
  6. <RegionCode>06</RegionCode>   
  7. <RegionName>California</RegionName>   
  8. <City>Mountain View</City>   
  9. <ZipPostalCode>94043</ZipPostalCode>   
  10. <Latitude>37.4192</Latitude>   
  11. <Longitude>-122.057</Longitude>   
  12. <Gmtoffset>-28800</Gmtoffset>   
  13. <Dstoffset>0</Dstoffset>   
  14. <TimezoneName>America/Los_Angeles</TimezoneName>   
  15. <Isdst>0</Isdst>   
  16. <Ip>74.125.45.100</Ip>   
  17. </Response>  
Attached is the sample application for download.
Download Source Code GeoLocation (1 KB)