NOTE: This is currently broken, since I upgrade wordpress, seemingly due to the MIME plugin not working. Hopefully I’ll get round to fixing it. (either the page or the plugin). I should probably be doing this differently anyway.

 

My first “mashup”! It should geolocate your IP (using GeoIP from MaxMind), and then display a Google map of the approximate location. Note this is a proper google map you can move around and stuff. Since it’s a free version of the database, it’s sadly lacking, and may well just think you are in the middle of Kansas.
)

Technically not really a mashup, since it only uses one web service (the IP lookup is hosted server-side, using the free database), but I’m going to count it. It took maybe an hour of fiddling with paths and suchlike to get something to appear. It’s the first PHP I’ve “written”.

Wordpress needed a bit of tweaking to get it to work, basically needing the runPHP plugin, and the Mime plugin. Not for the technically challenged. Before I forget what I did, I should write how it works

Step 1: download the files geoip.inc, geoipcity.inc and geoipregionvars.php from: http://www.maxmind.com/download/geoip/api/php/, I stuck them in folder /geo

Step 2: download the GeoLiteCity binary database, http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz, and stick it in the same place (or wherever, see later).

Step 3: The code you need to get latitude and longitude from an IP address is:

include("/your/absolute/path/geo/geoipcity.inc");

$gi = geoip_open("/your/absolute/path/geo/GeoLiteCity.dat",GEOIP_STANDARD);

$record = geoip_record_by_addr($gi,getenv('REMOTE_ADDR'));

print $record->latitude . ",";

print $record->longitude . "\n";

geoip_close($gi);

So all you need to do is stick that code (between <?php and ?>) where the latitude and longitude are in the sample the Google gives you when you sign up for the Google Maps API. Oh, and note the “/your/absolute/path/” will need changing appropriately.

There you go. It’s a nasty hack and I’ll tidy it up later. I think the first thing would be figuring out how to set document variable in PHP, so I can use the $record results in more than one place.





These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Reddit
  • Technorati
  • Fark
  • StumbleUpon
  • del.icio.us
  • Mixx
  • Google
  • blinkbits