Class: StaticGmaps::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/static_gmaps.rb

Constant Summary collapse

MAXIMUM_URL_SIZE =
1978
MAXIMUM_MARKERS =
50
DEFAULT_CENTER =
[ 0, 0 ]
DEFAULT_ZOOM =
1
DEFAULT_SIZE =
[ 500, 400 ]
DEFAULT_MAP_TYPE =
:roadmap
DEFAULT_KEY =
'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA'
DEFAULT_MARKERS =
[ ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Map

Returns a new instance of Map.



45
46
47
48
49
50
51
52
# File 'lib/static_gmaps.rb', line 45

def initialize(options = {})
  self.center   = options[:center]   || DEFAULT_CENTER
  self.zoom     = options[:zoom]     || DEFAULT_ZOOM
  self.size     = options[:size]     || DEFAULT_SIZE
  self.map_type = options[:map_type] || DEFAULT_MAP_TYPE
  self.key      = options[:key]      || DEFAULT_KEY
  self.markers  = options[:markers]  || DEFAULT_MARKERS
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def center
  @center
end

#keyObject

Returns the value of attribute key.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def key
  @key
end

#map_typeObject

Returns the value of attribute map_type.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def map_type
  @map_type
end

#markersObject

Returns the value of attribute markers.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def markers
  @markers
end

#sizeObject

Returns the value of attribute size.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def size
  @size
end

#zoomObject

Returns the value of attribute zoom.



38
39
40
# File 'lib/static_gmaps.rb', line 38

def zoom
  @zoom
end

Instance Method Details

#heightObject



58
59
60
# File 'lib/static_gmaps.rb', line 58

def height
  size[1]
end

#markers_url_fragmentObject



86
87
88
89
90
91
92
# File 'lib/static_gmaps.rb', line 86

def markers_url_fragment
  if markers && markers.any?
    return markers.collect{|marker| marker.url_fragment }.join('|')
  else
    return nil
  end
end

#to_blobObject



94
95
96
97
# File 'lib/static_gmaps.rb', line 94

def to_blob
  fetch
  return @blob
end

#urlObject

Raises:

  • (MissingArgument)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/static_gmaps.rb', line 63

def url
  raise MissingArgument.new("Size must be set before a url can be generated for Map.") if !size || !size[0] || !size[1]
  raise MissingArgument.new("Key must be set before a url can be generated for Map.") if !key
  if !(markers && markers.size > 1)
    raise MissingArgument.new("Center must be set before a url can be generated for Map (or multiple markers can be specified).") if !center
    raise MissingArgument.new("Zoom must be set before a url can be generated for Map (or multiple markers can be specified).") if !zoom
  end
  raise "Google will not display more than #{MAXIMUM_MARKERS} markers." if markers && markers.size > MAXIMUM_MARKERS
  parameters = {}
  parameters[:size]     = "#{size[0]}x#{size[1]}"
  parameters[:key]      = "#{key}"
  parameters[:map_type] = "#{map_type}"               if map_type
  parameters[:center]   = "#{center[0]},#{center[1]}" if center
  parameters[:zoom]     = "#{zoom}"                   if zoom
  parameters[:markers]  = "#{markers_url_fragment}"   if markers_url_fragment
  parameters = parameters.to_a.sort { |a, b| a[0].to_s <=> b[0].to_s }
  parameters = parameters.collect { |parameter| "#{parameter[0]}=#{parameter[1]}" }
  parameters = parameters.join '&'
  x = "http://maps.google.com/staticmap?#{parameters}"
  raise "Google doesn't like the url to be longer than #{MAXIMUM_URL_SIZE} characters.  Try fewer or less precise markers." if x.size > MAXIMUM_URL_SIZE
  return x
end

#widthObject



54
55
56
# File 'lib/static_gmaps.rb', line 54

def width
  size[0]
end