In this article I will explain how to get the Visitor’s Location details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using his IP Address.
 
IP Address to Location details API
Here I am using the IP Location API from IPInfoDB which has the following web service which provides location details using IP Address
The above URL returns data as JSON string
{
      "statusCode" : "OK",
      "statusMessage" : "",
      "ipAddress" : "74.125.45.100",
      "countryCode" : "US",
      "countryName" : "UNITED STATES",
      "regionName" : "CALIFORNIA",
      "cityName" : "MOUNTAIN VIEW",
      "zipCode" : "94043",
      "latitude" : "37.3861",
      "longitude" : "-122.084",
      "timeZone" : "-07:00"
}
 
 
Register and get API Key
In order to use this API you will have to first register and get API Key using the URL below
 
HTML Markup
The HTML markup contains ASP.Net GridView control which will display the fetched location details for the queried IP Address.
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
        <asp:BoundField DataField="CountryName" HeaderText="Country" />
        <asp:BoundField DataField="CityName" HeaderText="City" />
        <asp:BoundField DataField="RegionName" HeaderText="Region" />
        <asp:BoundField DataField="CountryCode" HeaderText="Country Code" />
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Timezone" HeaderText="Timezone" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces
C#
using System.Net;
using System.Collections.Generic;
using System.Web.Script.Serialization;
 
VB.Net
Imports System.Net
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
 
 
Custom Business Class to hold the JSON data
The following properties class Location will hold the queried location details.
 
C#
 
public class Location
{
    public string IPAddress { get; set; }
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    public string CityName { get; set; }
    public string RegionName { get; set; }
    public string ZipCode { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string TimeZone { get; set; }
}
 
VB.Net
Public Class Location
    Public Property IPAddress() As String
        Get
            Return m_IPAddress
        End Get
        Set(value As String)
            m_IPAddress = value
        End Set
    End Property
    Private m_IPAddress As String
    Public Property CountryName() As String
        Get
            Return m_CountryName
        End Get
        Set(value As String)
            m_CountryName = value
        End Set
    End Property
    Private m_CountryName As String
    Public Property CountryCode() As String
        Get
            Return m_CountryCode
        End Get
        Set(value As String)
            m_CountryCode = value
        End Set
    End Property
    Private m_CountryCode As String
    Public Property CityName() As String
        Get
            Return m_CityName
        End Get
        Set(value As String)
            m_CityName = value
        End Set
    End Property
    Private m_CityName As String
    Public Property RegionName() As String
        Get
            Return m_RegionName
        End Get
        Set(value As String)
            m_RegionName = value
        End Set
    End Property
    Private m_RegionName As String
    Public Property ZipCode() As String
        Get
            Return m_ZipCode
        End Get
        Set(value As String)
            m_ZipCode = value
        End Set
    End Property
    Private m_ZipCode As String
    Public Property Latitude() As String
        Get
            Return m_Latitude
        End Get
        Set(value As String)
            m_Latitude = value
        End Set
    End Property
    Private m_Latitude As String
    Public Property Longitude() As String
        Get
            Return m_Longitude
        End Get
        Set(value As String)
            m_Longitude = value
        End Set
    End Property
    Private m_Longitude As String
    Public Property TimeZone() As String
        Get
            Return m_TimeZone
        End Get
        Set(value As String)
            m_TimeZone = value
        End Set
    End Property
    Private m_TimeZone As String
End Class
 
 
Fetching and displaying the Location Details for the IP Address
First the IP Address of the visitor is determined; once the IP Address is available a query is made to the web service using the API Key and the IP Address. The response from the web service is deserialized to the object of class Location. Finally the fetch data is displayed in the ASP.Net GridView
C#
protected void Page_Load(object sender, EventArgs e)
{
    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ipAddress))
    {
        ipAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
 
    string APIKey = "<Your API Key>";
    string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
    using (WebClient client = new WebClient())
    {
        string json = client.DownloadString(url);
        Location location = new JavaScriptSerializer().Deserialize<Location>(json);
        List<Location> locations = new List<Location>();
        locations.Add(location);
        gvLocation.DataSource = locations;
        gvLocation.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If String.IsNullOrEmpty(ipAddress) Then
        ipAddress = Request.ServerVariables("REMOTE_ADDR")
    End If
 
    Dim APIKey As String = "<Your API Key>"
    Dim url As String = String.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress)
    Using client As New WebClient()
        Dim json As String = client.DownloadString(url)
        Dim location As Location = New JavaScriptSerializer().Deserialize(Of Location)(json)
        Dim locations As New List(Of Location)()
        locations.Add(location)
        gvLocation.DataSource = locations
        gvLocation.DataBind()
    End Using
End Sub
 
 
Demo
 
Downloads