Posted by admin Sun, 06 Dec 2009 07:05:00 GMT

While working on a project I found it necessary to generate some custom Google map markers and I wanted them numbered 1..999. I searched for a while and found a few php scripts and some snippets here and there, none of which did quite what I wanted. I decided to write my own. Here it is for your own mapping pleasure:

I saved mine as

gen_google_markers.rb
. This is quick and dirty but it does what I wanted. If you use it and make it better please send me a patch or just a note to let me know what you did so I can improve my version as well.

#!/usr/bin/env ruby

require 'rubygems'
require 'rmagick'
include Magick

WIDTH = 33
HEIGHT = 37
FONT_COLOR = "#000000"
MARKER_COLOR = "#CC0000"

def generate_marker(num, font_color, marker_color)
  throw "Number too big [#{num}], can only be 3 digits maximum." if num > 999
  canvas = Image.new(WIDTH, HEIGHT) {
    self.background_color = 'transparent'
    # self.background_color = 'white'
  }
  gc = Draw.new
  gc.fill_opacity(100)
  gc.fill(marker_color)
  gc.stroke('none')
  gc.stroke_width(1)

  # Draw triangle
  gc.path("M#{WIDTH*0.091},#{HEIGHT*0.433} L#{WIDTH/2},#{HEIGHT*0.966} L#{WIDTH*0.909},#{HEIGHT*0.433} z")

  # Draw ellipse on top of triangle
  gc.stroke(marker_color) # set stroke to fix the offset where elipse meets triangle
  gc.ellipse(WIDTH/2, HEIGHT*0.313, WIDTH*0.455, HEIGHT*0.267, 0, 360)

  # Annotate
  pointsize = (WIDTH*1.0/2) - 2
  gc.fill(font_color)
  gc.stroke('none')
  gc.pointsize = pointsize
  gc.gravity = NorthGravity
  gc.text_align(CenterAlign)
  gc.text_antialias(true)
  gc.text(WIDTH/2, (HEIGHT/2)-(HEIGHT/20), "#{num}")

  # Write file
  gc.draw(canvas)
  canvas.write("#{num}.png")
end

marker_color = ARGV.shift
font_color = ARGV.shift

for num in 1..999
  generate_marker(num, font_color, marker_color)
end

Happy mapping…