User Id :    Password :      New Member   Forgot Password  
 
How to get visitors Country, longitude, latitude from IP address is asp.net
Description Here I will show you to how to get visitor's Country,longitude,latitude from IP address is asp.net   No. of Views     19936
  Rating     1
Author Ahsan Murshed   Posted On     11 Jul 2010
Tags ASP.NET,C#,XML    

Sample Code   Download Code

Introduction

It is very common questions for new web developer how to get the client's country location to track the user.In various forum I have faced this questions. So that today I want to share how to get client's country location,longitude,latitude,country code etc. To get this you need to use a
free database which has IP to Location mapping
or
call a web service that does the same for you.

Using free web services is the easy way to get the location of the user based on its IP address. After goggling I have found the following web service which provide this service absolutely free and that too without any complex interface to do the same.

http://freegeoip.appspot.com/

The above website provides free IP Geolocation Web Service that returns data in three formats .


1. XML [Extended Markup Language]
2. CSV [Comma Separated Values]
3. JSON [JavaScript Object Notation]

Here I am explaining how to get the data in XML format. It is very easy to use this web service,just send your ip address through the URL
like:
http://freegeoip.appspot.com/xml/116.68.204.254

The returned XML with result are as below:

    	<response>     
		<status>true</status>     
		<ip>116.68.204.254</ip>     
		<countrycode>BD</countrycode>     
		<countryname>Bangladesh</countryname>     
		<regioncode>81</regioncode>     
		<regionname>Dhaka</regionname>     
		<city>Dhaka</city>     
		<zipcode></zipcode>     
		<latitude>23.723</latitude>     
		<longitude>90.4086</longitude>     
	</response>



How to consume this web service and get the result: Here WebRequest and WebProxy is responsible for make call this url and xml response is received by WebResponse and store it to dataset using XMLTextReader.

Code:

 

    
private DataTable GetGeoLocation(string ipaddress)
    {
    //Create a WebRequest
    WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/"
    + ipaddress);
    //Create a Proxy
    WebProxy px =new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);

    //Assign the proxy to the WebRequest
    rssReq.Proxy = px;

    //Set the timeout in Seconds for the WebRequest
    rssReq.Timeout = 2000;
    try
    { //Get the WebResponse
    WebResponse rep = rssReq.GetResponse();

    //Read the Response in a XMLTextReader
    XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

    //Create a new DataSet
    DataSet ds = new DataSet();

    //Read the Response into the DataSet
    ds.ReadXml(xtr);
    return ds.Tables[0];
    }
    catch
    {
    return null;
    }
    }



How to show result in aspx page:
You can place this code on Page_Load event or under any button event whenever you like:

    protected void ShowGeoLocatio()
    {
    string sIpaddress;
    sIpaddress= Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if(sIpaddress== "" || sIpaddress== null)
    {
    sIpaddress= Request.ServerVariables["REMOTE_ADDR"];
    }

    //call the function to consume web service and store result into datatable
    DataTable odtGeoLocation = GetGeoLocation(sIpaddress);
    if (odtGeoLocation != null)
    {
    if (odtGeoLocation .Rows.Count > 0)
    {
    lblCity.Text = odtGeoLocation .Rows[0]["City"].ToString();
    lblRegion.Text = odtGeoLocation .Rows[0]["RegionName"].ToString();
    lblCountry.Text = odtGeoLocation .Rows[0]["CountryName"].ToString();
    lblCountryCode.Text = odtGeoLocation .Rows[0]["CountryCode"].ToString();
    }
    else
    {
    lblError="Sorry,no data found!!";
    }
    }
    }


Hope that it may helps the developer.Thanks.

About Author

About Author I am working as Software engineer having 4+ years of experience in Microsoft Technologies, System Design and Integration and deployment, Search Engine Optimization (SEO). I have developed web based application in asp.net/C# and database design. My Site: http://www.dotnetboss.com My blog: http://aspboss.blogspot.com Ahsan
No Photo
 
Country N/A
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Shaheen on 25 Jul 2011 at 11:04 PM
 
Hello Ankit,
Client location is tracked through the internet connection and the communication devices like moden and router. So depending on the router location it shows you the client location.
 
 
 
Posted By Najmul Hoda on 25 Jul 2011 at 10:19 PM
 
Hi Ankit,
If this dont work for you please try this link.
http://ipaddressextensions.codeplex.com/

Hope this will help you.
 
 
 
Posted By Ankit Garg on 25 Jul 2011 at 10:36 AM
 
Hi,
API not working properly(http://freegeoip.appspot.com)
After publishing that code, it is providing different results when i check it from my landline connection and the same with my mobile device. when i use it from mobile , it connects to other countries routers and displays that countries latitude-longitude.
please help me
 
 
   
Write your comment here.
Comment
Verification Code