携帯でGoogleMapを手軽に表示するPHPクラス - アシアルブログがあったからrubyで書いてみました[ruby][web][rails]

#
#携帯でGoogleMapの画像をプリントするクラス
#
class GoogleMobileMapView 

  PKG_VERSION = '1.0.0'

  #
  #取得URLとクエリを保持する変数
  #
  GET_URL    = 'http://maps.google.com/mapprint?tstyp=4'
  #
  #文字列とパラメータimage_formatの対応
  #
  IMAGE_TYPE = {"gif"=>1,"png"=>2,"jpeg"=>3,"jpg"=>3}

  def initialize(latitude,longitude,settings = Hash.new,points = Array.new)
    @query = ""
    raise StandardError.new("ArgumentError") unless set_url(latitude,longitude,settings,points)
  end 
  
  private
  def set_url(latitude,longitude,settings = Hash.new,points = Array.new)
    #中心の位置がない場合はfalseを返す
    if not latitude or not longitude
      return false
    end 
 
    #緯度経度
    @query = GET_URL + "&c=#{longitude},#{latitude}"
    #幅 
    settings["w"] = 200 unless settings["w"]
    #高さ
    settings["w"] = 200 unless settings["h"]
    #拡大度?
    settings["z"] = 0   unless settings["z"]

    @query += "&r=#{settings["w"]},#{settings["h"]}"
    @query += "&z=#{settings["z"]}"

    if settings["image_format"] =~ /^[1-3]$/
      @image_format = settings["image_format"]
    elsif IMAGE_TYPE[settings["image_format"]]
      @image_format = IMAGE_TYPE[settings["image_format"]]
    else
      #出力形式をセット(デフォルトはGIF)
      @image_format = "1" 
    end 
    @query += "&image_format=#{@image_format}"

    points.each do |point|
      next if not point["latitude"] or not point["longitude"]
      point["iconid"] = 15 unless point["iconid"]
      @query += "&l=#{point["longitude"]},#{point["latitude"]},#{point["iconid"]}"
    end
    return true
  end

  def to_s
    @query
  end

end

if __FILE__ == $0
points =
[
    {'latitude' => 35707701,'longitude' => 139761787,'iconid' => 17},
    {'latitude' => 35706847,'longitude' => 139762618,'iconid' => 18},
    {'latitude' => 35706764,'longitude' => 139762515,'iconid' => 19},
]
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => '1'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => '2'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => '3'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => '4'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => 'gif'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => 'png'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => 'jpg'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => 'jpeg'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240,'image_format' => 'hoge'},points)
puts GoogleMobileMapView.new(35707215,139762162,{'w' => 240,'h' => 240},points)
#puts GoogleMobileMapView.new(nil,139762162,{'w' => 240,'h' => 240,'image_format' => '3'},points)
end