Class: StaticGmaps::Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Map

Returns a new instance of Map.



71
72
73
74
75
76
77
78
# File 'lib/static_gmaps.rb', line 71

def initialize(options = {})
  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]   || StaticGmaps::default_sensor
  self.markers  = options[:markers]  || [ ]
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def center
  @center
end

#map_typeObject

Returns the value of attribute map_type.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def map_type
  @map_type
end

#markersObject

Returns the value of attribute markers.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def markers
  @markers
end

#sensorObject

Returns the value of attribute sensor.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def sensor
  @sensor
end

#sizeObject

Returns the value of attribute size.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def size
  @size
end

#zoomObject

Returns the value of attribute zoom.



64
65
66
# File 'lib/static_gmaps.rb', line 64

def zoom
  @zoom
end

Instance Method Details

#heightObject



84
85
86
# File 'lib/static_gmaps.rb', line 84

def height
  size[1]
end

#markers_url_fragmentObject



117
118
119
120
121
122
123
# File 'lib/static_gmaps.rb', line 117

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

#to_blobObject



125
126
127
128
# File 'lib/static_gmaps.rb', line 125

def to_blob
  fetch
  return @blob
end

#urlObject

Raises:

  • (MissingArgument)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/static_gmaps.rb', line 89

def url
  raise MissingArgument.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 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 #{StaticGmaps::maximum_markers} markers." if markers && markers.size > StaticGmaps::maximum_markers
  parameters = {}
  parameters[:size]     = "#{size[0]}x#{size[1]}"
  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[:sensor]   = "#{sensor}"
  
  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/maps/api/staticmap?#{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



80
81
82
# File 'lib/static_gmaps.rb', line 80

def width
  size[0]
end