Friday, August 20, 2010

Gem for getting Google static maps

I've created a gem for getting maps from the Google Maps static API service called googlestaticmap. With static maps, you can specify parameters for a map, from image size, image type, and markers, path lines, and polygons to draw on the map, and in one http get to Google, retrieve the map. This is great for mobile sites where many visitors won't be able to use the Google Maps 2D API. It's also great if you have a map image that you want people to see, but don't want to load all of the Google maps javascript on your page.

To install the gem, simply type "gem install googlestaticmap" (the gem is on gemcutter, and I believe you need version 1.3.6 or higher of Rubygems to get gems from there). Documentation for the gem is at with several examples of how to use it. Also if you want to see the source, it's on Github at


Hans said...

but there is a typo in my comment
markers should be

Hans said...

Thanksfor your concern
I use ruby 1.8 but will change in a month to ruby 1.9

The exact names are for
cleaned_name Kalmar
county Kronobergs län
country Sverige

Another question: If I have a center and a marker and the zoom does not allow both to be visible what will then be displayed

Brent said...

Ah, I think I see what the problem is. This has nothing to do with the code or Ruby version. If you go to Google Maps and search for "Kalmar,Kronobergs län, Sverige", "Kalmar Kronobergs län, Sverige" or any other variation, the first match is somewhere far away from the city of Kalmar. Whereas if you just type Kalmar, Sverige you get the correct location. The marker put on the map for static maps is always the first match that Google finds for the address. Try leaving out the center location when creating the map and you'll see the marker.

To answer your second question, the map will always be centered at the center location that you specify. The map only centers on the marker location if you don't specify a center.

Hans said...

I am now upploading my aplication with your gem to a web hotell server and then I have a problem that can be related to your last version changes.
Locally everything works with ruby 1.8.6 On the server hotell they have ruby 1.8.7 Have you tested that version.
The server use
uby 1.8.7 (2010-08-16 patchlevel 302) [i486-linux]
I use locally
ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]

The server cannot find the gem but says
rom /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:158:in `require'
from /home/d19042/Disweb_1/app/models/place.rb:33
in the place model there is

require 'googlestaticmap'

Any idees?

Hans said...

Just a possible solution
Can googelstaticmap be downloaded asa plugin from github

Hans said...

Hi - problem solved
I just downloaded the GIT code as a plugin and now it works both on the server and locally. I will not test the gem version now but later on There were other problems with the deployement that might caused the problem with the gem. I will report back the result later

Brent said...

Yes, I have tried the latest version of the gem with Ruby 1.8.7, and it appears to be working fine for me. In fact I have tried it with Rails 2.3.8 which it looks like you are using, and require 'googlestaticmap' is working fine for me.

I don't think this would be your issue since you are doing this from a Rails app, but if you are writing a script with Ruby 1.8.7, you must first do a require 'rubygems'. For Ruby 1.9 you do not need to do this.

Brent said...

I've made a new version 1.1.0 of this gem. Changes since February are adding a scale and proxy server options. These two changes were submitted by others, thanks to everyone that sends pull requests to add your changes in, I appreciate it.

Brent said...

Version 1.1.1 is out with support for specifying the protocol to use to get the map.

Unknown said...

Hey Brent, loving the gem! I couldn't get access to the map data byte array. Could be my ruby version (1.9.3) - because I wasn't sure how the line 175 would work given the API for get2.

I forked your git repo at dusanb/googlestaticmap. It's a trivial change so would rather do a merge back onto your code if you're happy with it.

Ping me on dusanb devicemagic com if you are?