ipinfodb.com provides a free service wich let you obtain geolocation info about any ip address. you can access this info parsing the content returned by this page.
the request must be done in this way
http://ipinfodb.com/ip_query.php?timezone=true&ip=204.236.220.71
UPDATE
The above url is not longer valid because to a New APIs are being introduced with API key. you must register in this site to get access to the info.
now you must use a url like this
http://api.ipinfodb.com/v2/ip_query.php?key=&ip=74.125.45.100&timezone=true
and the response look like this
<Response> <Ip>204.236.220.71</Ip> <Status>OK</Status> <CountryCode>US</CountryCode> <CountryName>United States</CountryName> <RegionCode>53</RegionCode> <RegionName>Washington</RegionName> <City>Seattle</City> <ZipPostalCode>98144</ZipPostalCode> <Latitude>47.5839</Latitude> <Longitude>-122.299</Longitude> <TimezoneName>America/Los_Angeles</TimezoneName> <Gmtoffset>-28800</Gmtoffset> <Isdst>0</Isdst> </Response>
Now using a TIdHTTP component you can get the result of the request to this page.
uses
Classes,
ComObj,
Variants,
IdHTTP,
type
TGeoInfo = record
Status : string;
CountryCode : string;
CountryName : string;
RegionCode : string;
City : string;
ZipPostalCode : string;
Latitude : string;
Longitude : string;
TimezoneName : string;
Gmtoffset : string;
Isdst : string;
end;
const
UrlGeoLookupInfo ='http://ipinfodb.com/ip_query.php?timezone=true&ip=%s';
UrlGeoLookupInfo2 ='http://backup.ipinfodb.com/ip_query.php?timezone=true&ip=%s'; //backup server
procedure GetGeoInfo(const IpAddress : string;var GeoInfo :TGeoInfo);
var
lHTTP : TIdHTTP;
lStream: TStringStream;
XMLDoc : OleVariant;
ANode : OleVariant;
begin
lHTTP := TIdHTTP.Create(nil);
lStream := TStringStream.Create;
try
try
lHTTP.Get(Format(UrlGeoLookupInfo,[IpAddress]), lStream); //get the request
except
lHTTP.Get(Format(UrlGeoLookupInfo2,[IpAddress]), lStream); //if something is wrong try using the backup server.
end;
lStream.Seek(0,0);
XMLDoc := CreateOleObject('Msxml2.DOMDocument.6.0');
XMLDoc.async := false;
XMLDoc.LoadXML(lStream.ReadString(lStream.Size));
XMLDoc.setProperty('SelectionLanguage','XPath');//use XPath to parse the xml result
ANode:=XMLDoc.selectSingleNode('/Response/Status');
if not VarIsNull(ANode) then GeoInfo.Status:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/CountryCode');
if not VarIsNull(ANode) then GeoInfo.CountryCode:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/CountryName');
if not VarIsNull(ANode) then GeoInfo.CountryName:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/RegionCode');
if not VarIsNull(ANode) then GeoInfo.RegionCode:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/City');
if not VarIsNull(ANode) then GeoInfo.City:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/ZipPostalCode');
if not VarIsNull(ANode) then GeoInfo.ZipPostalCode:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/Latitude');
if not VarIsNull(ANode) then GeoInfo.Latitude:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/Longitude');
if not VarIsNull(ANode) then GeoInfo.Longitude:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/TimezoneName');
if not VarIsNull(ANode) then GeoInfo.TimezoneName:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/Gmtoffset');
if not VarIsNull(ANode) then GeoInfo.Gmtoffset:=ANode.Text;
ANode:=XMLDoc.selectSingleNode('/Response/Isdst');
if not VarIsNull(ANode) then GeoInfo.Isdst:=ANode.Text;
finally
lHTTP.Free;
lStream.Free;
end;
end;























