Pages

Wednesday, 22 May 2013

How To Get User IP Address, Latitude, Longitude, Country, City, State Details using Javascript

How To Get User IP Address, Latitude, Longitude, Country, City, State Details using JavaScript


Program:

.Aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ipdetail.aspx.cs" Inherits="ipdetail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>IP Detail</title>
    <script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript">
    var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
    function GetUserInfo(data) {
        strip = data.host; strcountry = data.countryName; strcity = data.city;
        strregion = data.region; strlatitude = data.latitude; strlongitude = data.longitude;
        strtimezone = data.timezone;
    }
    $(function () {
        BindUserInfo();
    })
    function BindUserInfo() {
        document.getElementById('lblIP').innerHTML = strip;
        document.getElementById('lblCountry').innerHTML = strcountry;
        document.getElementById('lblCity').innerHTML = strcity;
        document.getElementById('lblregion').innerHTML = strregion;
        document.getElementById('lbllatitude').innerHTML = strlatitude;
        document.getElementById('lbllongitude').innerHTML = strlongitude;
        document.getElementById('lbltimezone').innerHTML = strtimezone;
    }
</script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table id="tbDetails" cellpadding="2" cellspacing="2" style=" border:1px solid #000; font-family:Verdana;" >
<tr style="background-color:#DC5807; color:White; font-weight:bold">
<td colspan="2" align="center">User Information</td>
</tr>
<tr style="border:solid 1px #000000">
<td align="right"><b>IP:</b></td>
<td><label id="lblIP"/></td>
</tr>
<tr>
<td align="right"><b>Country:</b></td>
<td><label id="lblCountry"/></td>
</tr>
<tr>
<td align="right"><b>City:</b></td>
<td><label id="lblCity"/></td>
</tr>
<tr>
<td align="right"><b>Region:</b></td>
<td><label id="lblregion"/></td>
</tr>
<tr>
<td align="right"><b>latitude:</b></td>
<td><label id="lbllatitude"/></td>
</tr>
<tr>
<td align="right"><b>Longitude:</b></td>
<td><label id="lbllongitude"/></td>
</tr>
<tr>
<td align="right"><b>Time Zone:</b></td>
<td><label id="lbltimezone"/></td>
</tr>
</table>
    </div>
    </form>
</body>
</html>

Aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ipdetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Demo:

How To Get User IP Address, Latitude, Longitude, Country, City, State Details using Javascript
How To Get User IP Address, Latitude, Longitude, Country, City, State Details using Javascript


No comments:

Post a Comment