Well, some time back I posted an article on How to retrieve the IP Address of a user in ASP.Net. This article can be considered an extension to the same as we will discuss a bit about Geolocation.
Geolocation used to be one of the most costly and tricky implementation a few years ago. Today this is one of the most wanted feature by many of the customers and in fact is widely used by a lots of sites.
The information translated from the IP Address is generally the Country, City, Zip Code etc. This is very helpful information in a lot of scenarios like analytics, customizing the software functionality (language, currency etc) according to his location.
There are a lot of sites which offer this as a paid service. I would not touch any of them :), but a couple of community based and free services.
HostIP.info is one of the community based IP addresses, making the database freely available. HostIP.info offers the result in a xml format which can be read by your application. Below is an example to use this service from C# for retrieving City and Country.
//Get info from HostIp.info
XmlTextReader reader = new XmlTextReader("http://api.hostip.info/?ip=" + _IPAddress);
String _city = String.Empty;
String _country = String.Empty;
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.Name == "gml:name")
{
_city = reader.ReadString();
}
if (reader.Name == "countryName")
{
_country = reader.ReadString();
}
}
}
GEO I/O is another service which offers a free account for the users who make up to 1000 queries per day. GEO I/O offers the result in a comma or pipe separated formats.
//Get info from GEO I/O
WebClient LobjWebClient = new WebClient();
string LstrData = LobjWebClient.DownloadString(string.Format("http://api.geoio.com/q.php?key=xxxx{0}", _IPAddress));
string[] LstrSplitData = LstrData.Split('|');
Note : Every user gets his own query key after creation of account with GEO I/O.
//GEO I/P Service returns the string in the following order of seperated string(you can configure your account for the type of seperator)
// 1 - City
// 2 - State
// 3 - Country
// 4 - ISP
// 5 - Longitude
// 6 - Latitude
_city = LstrSplitData[0];
_Country = LstrSplitData[2];
Be First to Comment