Google Maps is, may be, the coolest service for integrating maps in your web applications: it offers a powerful and easy API with no limit usage.
The problem is that it loads quite Javascript that has to connect to the main Google Maps page in order to draw our map. In order to avoid that we can use static Google Maps, it is, a picture (generated in real time) of the map we want to visualize. The problem with this is that you can't interact with the map (because it is an image) and that there is a limitation per day. But may be you find it useful in a particular moment.
For example, in iwannagothere.net we have a small map in every place that shows the location of that city:

In that situation we can change the map because the map is not for navigating along the city. For that you have a map in each item.
In Ruby on Rails, the programming framework of iwannagothere.net there is a gem called static-gmaps that does the hard work for you. What's more, we have found the way to integrate this maps with attachment_fu, in order you save a copy of the map in your filesystem and then avoid the day limitation usage.
Basically we have an attachment_fu kind of model (nothing special here):
class PlaceMap < ActiveRecord::Base belongs_to :place has_attachment :content_type => :image, :storage => :file_system, :processor=> :rmagick, :path_prefix => 'public/userfiles/place_maps', :max_size => 1.megabyte validates_as_attachment end
The we generate the static map instance:
map = StaticGmaps::Map.new :center => [ place.lat, place.long ], :zoom => 12, :size => [ 334, 144 ], :key => APP['gmkey']
And save it in our PlaceMap model:
PlaceMap.create(:uploaded_data => (open(map.url)), :place_id => place.id, :content_type => 'image/gif')
The problem here is that we are not uploading a file in the usual way: we are trying to save a raw of data. Attachment_fu doesn't know how to do that, so we have to apply a little patch:
module Technoweenie module AttachmentFu module InstanceMethods def uploaded_data=(file_data) return nil if file_data.nil? || file_data.size == 0 self.content_type = file_data.content_type self.filename = Time.now.to_i.to_s+'.gif' if file_data.is_a?(StringIO) file_data.rewind self.temp_data = file_data.read else self.temp_path = file_data end end end end end
Here we are giving a random filename, generated from the current time.
And that's all, really easy :)


