Rails Optimize Google Maps Load Times

Ain’t it grand when you get to something on your list that has been getting put off for months, only to discover that somebody did it for you a few days ago?

Such was the case with us and our Google Maps. We had long intended to cache Google Maps (accessed through the YM4R plugin) on account of the 1-2 seconds per page load that the maps cost (!). Most of that time is spent running JS — both from Google (that grabs the map tiles and sets up zooming, scrolling, etc.) and from YM4R (don’t know what that JS is doing, but it’s costly).

Anyway, if you are putting maps on your page, and you don’t need the user to be able to move the map around, don’t be a fool: use static Google Maps and take your load time from 2 seconds to .02 seconds.

And being that we are in the midst of a Rails revolution, it should surprise no one that within 48 hours of Google unveiling the static map service (in late February) someone (<credit>John Wulff</credit>) had already written a gem to use it in Ruby. Unfortunately, gems suck, especially gems like this one with multiple gem dependencies. But there is no reason you can’t just get the functionality as a Rails plugin. Here is the file. All you have to do is put it in your /vendor/plugins directory under something like vendor/plugins/static_gmaps/lib and you’re golden. Usage for the static map is like so:

@map = StaticGmaps::Map.new(:center => [ @lat, @lng ],
:zoom => 10, # gmaps zoom level
:size => [ 120, 75 ], # pixel width, height
:map_type => :mobile, #:mobile or :roadmap, :mobile is a bit more distinct for small maps

Then, you put it in an image tag:


Now, I do say, that is easy way to cut out 2 seconds/page load.

Update: Almost forgot to mention one painful lesson I learned when implementing the static maps, which is that, unlike with the YM4R maps, Google does check your HTTP_REFERER when using static maps. So, to all you localhosts: don’t forget to go to Google Maps and grab an API key that works for the IP address in your HTTP_REFERER string. If your referer string doesn’t match your API key all you’ll get is a blank image and no explanation.

2 Replies to “Rails Optimize Google Maps Load Times”

  1. Glad to hear you found static-gmaps useful!

    What multiple dependencies are you talking about? RSpec? You only need RSpec if you want to run the tests.

    And for the record: gems rock

    sudo gem install static-gmaps

  2. When I installed the gem, it grabbed two other gems as dependencies: one was hoe, I don’t remember what the other was.

    The main thing that I have against gems is that they require each and every Rails installation to install the gem. We have about five developers, plus two workstations for myself, plus our two deployment boxes. When we install a new gem that means that I have to install it in four different places, and add it to our setup instructions on our wiki as yet another step. Oh, and lord help you if you have to upgrade or re-install Ruby, cuz the Ruby uninstaller (required for upgrade) kills the Ruby directory, and with it, each and every one of those gems you previously installed.

    Compare that to using it as a Rails plugin, where we just drop it into SVN and nobody even knows it was added.

    I definitely like the *idea* of gems, and installing it on one workstation is simple as can be, but the longer I work with them, and the more complex our deployment scenario becomes, the more I appreciate being able to drop something into SVN as a Rails plugin and forget about it.

Leave a Reply

Your email address will not be published. Required fields are marked *