Hi nauna,
Instead of using Google API, there is a free API available to get Geographic Location (GeoLocation) based on the ip address without using any key.
FreeGeoIP
FreeGeoIP API which returns the Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using the IP Address.
HTML
Postal / Zip Code: <b><asp:Label ID="lblPostalCode" runat="server" /></b>
C#
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress = "122.170.0.23";
string url = string.Format("http://freegeoip.net/json/{0}", ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new Location();
location = new JavaScriptSerializer().Deserialize<Location>(json);
lblPostalCode.Text = location.Zip_Code;
}
}
public class Location
{
public string IP { get; set; }
public string Country_Code { get; set; }
public string Country_Name { get; set; }
public string Region_Code { get; set; }
public string Region_Name { get; set; }
public string City { get; set; }
public string Zip_Code { get; set; }
public string Time_Zone { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string Metro_Code { get; set; }
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ipAddress As String = "122.170.0.23"
Dim url As String = String.Format("http://freegeoip.net/json/{0}", ipAddress)
Using client As New WebClient()
Dim json As String = client.DownloadString(url)
Dim location As New Location()
location = New JavaScriptSerializer().Deserialize(Of Location)(json)
lblPostalCode.Text = location.Zip_Code
End Using
End Sub
Public Class Location
Public Property IP() As String
Get
Return m_IP
End Get
Set(value As String)
m_IP = value
End Set
End Property
Private m_IP As String
Public Property Country_Code() As String
Get
Return m_Country_Code
End Get
Set(value As String)
m_Country_Code = value
End Set
End Property
Private m_Country_Code As String
Public Property Country_Name() As String
Get
Return m_Country_Name
End Get
Set(value As String)
m_Country_Name = value
End Set
End Property
Private m_Country_Name As String
Public Property Region_Code() As String
Get
Return m_Region_Code
End Get
Set(value As String)
m_Region_Code = value
End Set
End Property
Private m_Region_Code As String
Public Property Region_Name() As String
Get
Return m_Region_Name
End Get
Set(value As String)
m_Region_Name = value
End Set
End Property
Private m_Region_Name As String
Public Property City() As String
Get
Return m_City
End Get
Set(value As String)
m_City = value
End Set
End Property
Private m_City As String
Public Property Zip_Code() As String
Get
Return m_Zip_Code
End Get
Set(value As String)
m_Zip_Code = value
End Set
End Property
Private m_Zip_Code As String
Public Property Time_Zone() As String
Get
Return m_Time_Zone
End Get
Set(value As String)
m_Time_Zone = value
End Set
End Property
Private m_Time_Zone 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 Metro_Code() As String
Get
Return m_Metro_Code
End Get
Set(value As String)
m_Metro_Code = value
End Set
End Property
Private m_Metro_Code As String
End Class
Output
Postal / Zip Code: 400601