Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Getting longitude and latitude from an IP address

by Kickstart (Pilgrim)
on Jan 05, 2002 at 04:43 UTC ( #136438=CUFP: print w/replies, xml ) Need Help??

Yeah, it's basically a web-page parsing script, but this is still a quick and dirty useful tutorial on how to do that. Given an IP address or set of IP addresses (@x), it will spew back the latitude and longitude that NetGeo has for it (approximates only).
@x =(''); #$/; foreach $ipaddress (@x) { $command = ' +?method=ge tLatLong\&target='.$ipaddress.'\&.cgifields=method\&.cgifields=nonbloc +king'; @foo = `lwp-request $command`; for $line (@foo) { chomp $line; $line =~ s/<br>//g; if ($line =~ /^(LAT:)/) { $lat = $line; $lat =~ s/^LAT://g; if ($lat < 0) {$lat = int($lat+180)} else {$lat = int($lat+0)} } elsif ($line =~ /^(LONG:)/) { $long = $line; $long =~ s/^LONG://g; if ($long < 0) {$long = int($long+180)} else {$long = int($long+0)} } } } print "Lat: $lat\tLong: $long";

This is from quite a while ago, but still works fine.


Replies are listed 'Best First'.
(Ovid) Re: Getting longitude and latitude from an IP address
by Ovid (Cardinal) on Jan 05, 2002 at 05:47 UTC

    Hey, fun script! Hope you don't mind, but I figured I would tighten it up a bit, add strict, and make it cross-platform compatible by using LWP::Simple.

    #!/usr/bin/perl -w use strict; use LWP::Simple; my @x =(''); foreach my $ipaddress (@x) { my $url = "". "method=getLatLong&target=$ipaddress&.cgifields=method&.cgifie +lds=nonblocking"; my $result= get( $url ); my ( $lat, $long ) = ( $result=~ /LAT:\s+(-?\d+\.\d+).*LONG:\s+(- +?\d+\.\d+)/s ); $lat += 180 if $lat < 0; $long += 180 if $long < 0; print "Address: $ipaddress\tLat: $lat\tLong: $long\n"; }


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      hai,i have no technical script knowledge. But i can get geo location of an ip address easily at ..
Re: Getting longitude and latitude from an IP address
by dws (Chancellor) on Jan 05, 2002 at 04:55 UTC
    Given direct access to a GPS, you could optimize the lookup for

    Seriously, though, since a substantial chunk of the IP addresses one typically sees in a server log are dynamically allocated (e.g., via @home or, or are funnelled via NAT through a single IP address that may be far distant from the source, the latitude and longitude this produces should be considered to be "for amusement purposes only".

      I wholeheartedly agree, especially with such things as AOL's users all funnelling through Virginia. But, if I really want to know where someone was I'd try for other methods. As I's a fairly simple page-parsing script, more of use as something for newbie perl people to scan than anything else.


Re: Getting longitude and latitude from an IP address
by mojotoad (Monsignor) on Jan 05, 2002 at 22:58 UTC
    There is an interesting article in the International Herald Tribune today that discusses the impending balkanization of the internet using tools such as your script (i.e., servers denying access based on geographical location).

    Im not convinced it will become as widespread as the author seems to believe, but its a good read.


    Update: The original IHT article, from Jan 4, 2002, can now only be found on The Washington Post archives although it requires a pay-per-whack account for the full fetch. :-(

      I suspect it's already happening...I once wrote a script for a girlfriend who wrote an online journal that sent visitors to various pages depending on their IP address (well, the first two parts anyway - testing for ISP). This allowed her to send her parents and other relatives off to a page telling them to screw off! :)

      The thing is...who knows? It's hard to tell how many people are doing this sort of thing. I suspect that I wouldn't mind terribly much if I went to (for example) eBay and all the prices were in Canadian dollars (since I'm in Vancouver). I might get a little annoyed if I went to Google and my searches came up only with Canadian sites though. It would remove the effectiveness of the service, rather than enhancing it.


      Anecdotal information relating to this: back in college I was trying to update my MS-IE to handle 128-bit crypto and was denied the download by the Microsoft website; some script on their end had used my IP to (incorrectly)determine my location to be outside the US. This was perhaps 5-6 years ago, when US companies couldn't export 128bit software. (That has changed, hasn't it?)

      setenv EXINIT 'set noai ts=2'

        Unless you're in Afghanistan, Iraq, or some other place like that... yup. There are still some restrictions but they are a little more relaxed now (to the best of my knowledge).

Re: Getting longitude and latitude from an IP address
by blakem (Monsignor) on Jan 05, 2002 at 06:31 UTC
    Why are you adding 180 to negative values? For one thing the sign on those numbers is important... for another, -10W does not equal 170E.... Whatever you were attempting to do, I think abs($long) would be better than $long+180.


Re: Getting longitude and latitude from an IP address
by elwarren (Curate) on Jan 10, 2002 at 01:25 UTC
    I wish I could contribute more than, "that's cool!" but gosh darnit, it is cool. This should be put into a module and released on CPAN.

    People have been putting lat/long info into dns records for awhile now, but it's never really caught on. I've been recently thinking about this since wireless usage has been growing.

    Does anyone know how the site does it's location verification? Does it actually do any?
Re: Getting longitude and latitude from an IP address
by Anonymous Monk on Jan 19, 2003 at 08:31 UTC
    You know NetGeo has a a perl module available on thier site that will do all the work for you without having to parse thier page. The NetGeo API includes functions take arguments that are either ASN's, ip addr's, or hostnames. Some of the available functions are: getLatLon, getCountry, getRecord, etc.
Re: Getting longitude and latitude from an IP address
by Hacker1 (Initiate) on Dec 12, 2011 at 03:35 UTC

    Hi All,

    I tried the original version by Kickstart but got no output. Just "Lat: Long:" with no values.
    I got no errors when running the code in the perl section of my webpage.
    I used my Ip address as a test.
    Any ideas?


      What makes you think IP addresses have a fixed location? And what makes you think the "location" of the IP address has any relation to the location of the user?

      One of my former employers had a central and mandatory proxy at the headquarter, so each and every user accessing the internet appeared to be located at the headquarter, even if s(he) was several hundred km away.

      At my current employer, I have to live with a partly broken proxy that I sometimes bypass using an SSH tunnel to a different computer. Whenever I do so, my "location" according to my current IP address jumps into a completely different state more than 30 km away, while I do not move a single millimeter.

      Things become even more funny when you use a mobile computer with an UMTS or GPRS card.


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Hi. I have several notes

      • Kickstart hasn't visited in 44 weeks, the post you replied to is 10 years old, and its real easy to miss a reply like yours
      • AFAIK, the original by kickstart as posted is broken. (Ovid) Re: Getting longitude and latitude from an IP address would be worth trying...
      • ... but the url in question has been gone for a long time, but even in 2002, there was a notice that this database was unmaintained/unreliable
      • Like mentioned in Mapping visitors to my site, you should go with Geo::IP cpan Geo::IP

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://136438]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-02-21 12:18 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.