How to cache google maps geocodes

Script last updated on August 16, 2007 1:34pm by !nucleo.

I've been working a lot with the google maps API lately. It's fun working with and is one of the easier API's I've worked with. One nice feature of the map system is the ability to geocode variables, which can be done on the fly by the viewers browser while s/he looks at the page.

Unfortunately there is a limit to 500k geocodes per day.

With the site I am working with, and the number of items displayed on the page, that would limit me to 600 page views per day, far below the expected turnout (1000x smaller than the expected pageviews on an upcoming project as well!).

After searching through the documentation I found a URL schema which returns latitude longitude pairs for addresses using the address and API key as input. This returns either XML, CSV or JSON. CSV is all I need for this simple project (saves bandwidth at least).

Here is the script I came up with to geocode an entire database of addresses. This can be altered a lot to suite your needs. For example, when a new address is added to the DB, you would only need to geocode that particular address. This script includes no error handling and is meant to be as simple as possible.

Released under the GPL, have fun with it (-: 

<?php
include("MYSQL CONNECTION FILE");

$sql = "SELECT id, address FROM locations";
$api_key = "API KEY";
$city_string = ",+City,+ST+ZZZZZ";

$result = runQuery($sql);

while ($row = mysql_fetch_assoc($result)) {
    $address = urlencode($row['address']);
    $url = "http://maps.google.com/maps/geo?q=
        {$address}{$city_string}&output=csv&key={$api_key}";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $document = curl_exec($ch);
    curl_close($ch);
    echo "<div><b>{$row['address']}</b> - $document</div>\n";
    list($r, $a, $lat, $lon) = explode(",",$document);
    runQuery("UPDATE locations SET latitude = '$lat',
        longitude = '$lon' WHERE id = {$row['id']} LIMIT 1");
   
}

Login

Username:
Password:

Shoutbox

By !nucleo
Jun 12, 2008 7:56pm
About time you're back... Shoot me an email with your 88x31.
By ?Lee
Jun 12, 2008 3:27pm
vimixx.net has moved to lee-stewart.co.uk!!!
By ?Medvedko
May 4, 2008 7:04am
That's right. Sorry, I am not making much sense, am I? Basically, I created the DB myself instead of using your install script. All necessary tables are there, I added few more based on plugin POST options and added those to the update.php also. At the beginning I added mysql.ssi.php function to both update and winamp_playing scripts as I had some troubles before with include_once.
By !nucleo
May 2, 2008 9:56am
Now I'm really confused, are you modifying my shoutbox script?
By ?Medvedko
Apr 30, 2008 1:08am
another thing is that update script terminates with invalid password (I added the pass in the Additional Options in the plugin and set the pass in the update script.
By ?Medvedko
Apr 30, 2008 1:06am
my fault really. I modded the scripts and added more tables to the DB such as "TIMESTAMP". I think include_once doesn't fetch the mysql function. Or atleast this is what seems to me is the problem.
Name: Shout: This Number: [View All]

Partners

Rand Affiliates

PixelXS Natayada Maximal Designs The Stash