#! /usr/bin/env ruby # # GoogleMapGenerator - auto-generate GoogleMap's html and javascript code # # # Version : 0.1.0 # Author : Kazuki Ohta # # Copyright (C) 2005-2006 Kazuki Ohta # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. module GoogleMapGenerator class LocationData class InvalidLocationDataError < StandardError; end attr_reader :label, :latitude, :longtitude, :description def initialize(label, latitude, longtitude, description) @label = label @latitude = latitude @longtitude = longtitude @description = description.sub(/\n/, "") check_data(@latitude) check_data(@longtitude) end private def check_data(data) if data =~ /\d+\.\d+/ return true else raise InvalidLocationDataError.new("invalid location data : " + data) end end end class Generator def initialize(apikey) @apikey = apikey @funcs = [] @handlers = [] @center_latitude = "" @center_longtitude = "" @center_zoom = 1 @width = 300 @height = 300 @control = false @markers = [] end def add_event_handler(raw_code) @handlers.push raw_code end def add_function(raw_code) @funcs.push raw_code end def set_center(latitude, longtitude, zoom) @center_latitude = latitude @center_longtitude = longtitude @center_zoom = zoom.to_s end def set_width(width) @width = width end def set_height(height) @height = height end def add_control @control = "map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl());" end def add_marker(location) @markers.push location end def gen_header "" end def gen_jsbody "
" end private def add_funcs_to_str ret = "" @funcs.each { |func| ret += func + "\n" } ret end def add_handlers_to_str ret = "" @handlers.each { |handler| ret += handler + "\n" } ret end def add_markers_to_str ret = "" @markers.each { |marker| ret += "addMarker( map, #{marker.longtitude}, #{marker.latitude}, '#{marker.description}' );\n" } ret end end end gen = GoogleMapGenerator::Generator.new($YOUR_GOOGLE_MAP_APIKEY) gen.set_center("-122.1419", "37.4419", 4) gen.add_control print "Content-Type: text/html\n\n" print " \n" print gen.gen_header print " \n" print gen.gen_jsbody print " "