Class: StaticGmaps::Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Map

Returns a new instance of Map.

Yields:

  • (_self)

Yield Parameters:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/static_gmaps2.rb', line 53

def initialize(options = {}, &block)
  self.center   = options[:center]
  self.zoom     = options[:zoom]     || StaticGmaps::default_zoom
  self.size     = options[:size]     || StaticGmaps::default_size
  self.map_type = options[:map_type] || StaticGmaps::default_map_type
  self.sensor   = options[:sensor]   || false
  self.key      = options[:key]
  self.format   = options[:format]
  self.markers  = options[:markers]  || [ ]
  yield self if block_given?
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def format
  @format
end

#keyObject

Returns the value of attribute key.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def key
  @key
end

#map_typeObject

Returns the value of attribute map_type.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def map_type
  @map_type
end

#markersObject

Returns the value of attribute markers.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def markers
  @markers
end

#sensorObject

Returns the value of attribute sensor.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def sensor
  @sensor
end

#sizeObject

Returns the value of attribute size.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def size
  @size
end

#zoomObject

Returns the value of attribute zoom.



45
46
47
# File 'lib/static_gmaps2.rb', line 45

def zoom
  @zoom
end

Instance Method Details

#centerObject



77
78
79
# File 'lib/static_gmaps2.rb', line 77

def center
  @center ? @center.value : nil
end

#center=(array_or_string) ⇒ Object



73
74
75
# File 'lib/static_gmaps2.rb', line 73

def center=(array_or_string)
  @center = Location.new(array_or_string)
end

#heightObject



69
70
71
# File 'lib/static_gmaps2.rb', line 69

def height
  size[1]
end

#markers_url_fragmentObject



111
112
113
114
115
116
117
# File 'lib/static_gmaps2.rb', line 111

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

#to_blobObject



119
120
121
122
# File 'lib/static_gmaps2.rb', line 119

def to_blob
  fetch
  return @blob
end

#urlObject

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/static_gmaps2.rb', line 82

def url
  raise ArgumentError.new("Size must be set before a url can be generated for Map.") if !size || !size[0] || !size[1]
  
  if(!self.center && !(markers && markers.size >= 1))
    self.center = StaticGmaps::default_center
  end
  
  if !(markers && markers.size >= 1)
    raise ArgumentError.new("Center must be set before a url can be generated for Map (or multiple markers can be specified).") if !center
    raise ArgumentError.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 #{StaticGmaps::maximum_markers} markers." if markers && markers.size > StaticGmaps::maximum_markers
  parameters = {}
  parameters[:size]     = "#{size[0]}x#{size[1]}"
  parameters[:key]      = "#{key}"                    if key
  parameters[:map_type] = "#{map_type}"               if map_type
  parameters[:center]   = "#{@center.to_s}" if center
  parameters[:zoom]     = "#{zoom}"                   if zoom
  parameters[:markers]  = "#{markers_url_fragment}"   if markers_url_fragment
  parameters[:sensor]   = "#{sensor}"
  parameters[:format]   = "#{format}"                 if format
  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 = "#{StaticGmaps.base_uri}?#{parameters}"
  raise "Google doesn't like the url to be longer than #{StaticGmaps::maximum_url_size} characters.  Try fewer or less precise markers." if x.size > StaticGmaps::maximum_url_size
  return x
end

#widthObject



65
66
67
# File 'lib/static_gmaps2.rb', line 65

def width
  size[0]
end