FirstClown

firstclown at firstclown.us

Google Map Issues

I recently did a Google Map for PodcampOhio. I had it set up so that I would just pass in a zip code to a GClientGeocoder and map that as a point. The problem was, if you're doing a lot of these at once, it would skip some at random. I wasn't sure why it was doing this, but I also saw the zip code look up for every request as pretty inefficient anyway. So I set up a function in PHP within a CodeIgniter Controller to cache the latitude and longitude for the zip codes (it's not like they're going to change):

function cache_new_zips(){
    $query = $this->db->query("SELECT ".
       "DISTINCT i.zip_code ".
       "from information i where i.zip_code NOT IN ".
       "(SELECT zip_code from zip_cache)");

    $results = $query->result_array();
    foreach($results as $result){

        $geocode_result = file_get_contents(
             "http://maps.google.com/maps/geo?q=".
             $result['zip_code'].
             "&output=csv&key=$key");

        $split_geocode_results =
             explode(',', $geocode_result);
        $lat = $split_geocode_results[2];
        $long = $split_geocode_results[3];

        $this->db->insert('zip_cache', array(
             'zip_code' => $result['zip_code'],
             'latitude' => $lat,
             'longitude' => $long));
    }
}

I can call this before every render of the map now. If a zip isn't in the cache table, it gets added. This really only happens when someone registers with a new zip, so it won't be all that often. Now I can map via the latitude and longitude, which is much more efficient and doesn't cause Google Maps to drop points.

Be sure to check out the map here: PodcampOhio Registration Map


Related posts:

  1. Google Maps Rocks I have a new mapping site: Google Maps. Google is...
  2. Does Google Index Hidden Divs? I was wondering recently if Google indexes hidden div tags....
  3. Fix for TiddlyWiki’s resolveTarget in IE I noticed the IE 6 wasn't happy when running the...

  • Joe
    I heard that too, but it appears to no longer be the case. The terms for the geocoding do say that you can use the geocode info only for Google Maps and not for any other application. That's fine by us since that's exactly what we're doing.
  • Not to be a party pooper, but you may want to verify if you can save those geocoded results and not violate the GMap terms of service. I may be getting crazy in my old age, but I seem to recall that you cannot save any of the data you get from GMaps. Yahoo's Geocoder, however, does not seem to have the same restrictions.


    Food for thought.
blog comments powered by Disqus

FirstClown is powered by WordPress
Entries (RSS) and Comments (RSS).