Class: GoogleStaticMapsHelper::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google_static_maps_helper/map.rb

Overview

The Map keeps track of the state of which we want to build a URL for. It will hold Markers and Paths, and other states like dimensions of the map, image format, language etc.

Constant Summary collapse

MAX_WIDTH =
640
MAX_HEIGHT =
640
VALID_FORMATS =
%w{png png8 png32 gif jpg jpg-basedline}
VALID_MAP_TYPES =
%w{roadmap satellite terrain hybrid}
REQUIRED_OPTIONS =
[:size, :sensor]
OPTIONAL_OPTIONS =
[:key, :center, :zoom, :format, :maptype, :mobile, :language]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Map

Creates a new Map object

:options

The options available are the same as described in Google’s API documentation. In short, valid options are:

:size

The size of the map. Can be a “wxh”, [w,h] or => x, :height => y

:sensor

Set to true if your application is using a sensor. See the API doc.

:center

The center point of your map. Optional if you add markers or path to the map

:zoom

The zoom level you want, also optional as center

:format

Defaults to png

:maptype

Defaults to roadmap

:mobile

Returns map tiles better suited for mobile devices with small screens.

:language

The language used in the map



38
39
40
41
42
43
44
45
# File 'lib/google_static_maps_helper/map.rb', line 38

def initialize(options = {})
  inject_defaults_from_module_class_attribute!(options)
  validate_required_options(options)
  validate_options(options)

  options.each_pair { |k, v| send("#{k}=", v) }
  @map_enteties = []
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



21
22
23
# File 'lib/google_static_maps_helper/map.rb', line 21

def height
  @height
end

#widthObject

Returns the value of attribute width.



21
22
23
# File 'lib/google_static_maps_helper/map.rb', line 21

def width
  @width
end

Instance Method Details

#<<(entity) ⇒ Object

Pushes either a Marker or a Path on to the map



106
107
108
109
110
# File 'lib/google_static_maps_helper/map.rb', line 106

def <<(entity)
  @map_enteties << entity
  @map_enteties.uniq!
  self
end

#eachObject



112
113
114
# File 'lib/google_static_maps_helper/map.rb', line 112

def each
  @map_enteties.each {|m| yield(m)}
end

#empty?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/google_static_maps_helper/map.rb', line 116

def empty?
  @map_enteties.empty?
end

#format=(format) ⇒ Object

Sets the format of the map

format

Can be any values included in VALID_FORMATS.

Raises:



190
191
192
193
# File 'lib/google_static_maps_helper/map.rb', line 190

def format=(format)
  @format = format.to_s
  raise UnsupportedFormat unless VALID_FORMATS.include? @format
end

#grouped_markersObject

Returns the markers grouped by it’s label, color and size.

This is handy when building the URL because the API wants us to group together equal markers and just list the position of the markers thereafter in the URL.



89
90
91
92
93
94
# File 'lib/google_static_maps_helper/map.rb', line 89

def grouped_markers
  markers.inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker|
    groups[marker.options_to_url_params] << marker
    groups
  end
end

#key=(key) ⇒ Object

Google static maps does no longer use key when a map is requested, thus this is deprecated.



209
210
211
212
# File 'lib/google_static_maps_helper/map.rb', line 209

def key=(key)# :nodoc:
  warn "[DEPRECATION] 'key' is deprecated. Key is no longer used when require a static map. Key will be removed from this gem soon!"
  @key = key
end

#key_without_warning=(key) ⇒ Object

Just a setter method for key which sets it without warning to be called from rspec so all tests still passes without warnings.

It’s just a couple of places we still test that key is working as it should.



222
223
224
# File 'lib/google_static_maps_helper/map.rb', line 222

def key_without_warning=(key)# :nodoc:
  @key = key
end

#lengthObject



120
121
122
# File 'lib/google_static_maps_helper/map.rb', line 120

def length
  @map_enteties.length
end

#maptype=(type) ⇒ Object

Sets the map type of the map

type

Can be any values included in VALID_MAP_TYPES.

Raises:



200
201
202
203
# File 'lib/google_static_maps_helper/map.rb', line 200

def maptype=(type)
  @maptype = type.to_s
  raise UnsupportedMaptype unless VALID_MAP_TYPES.include? @maptype
end

#marker(*args) ⇒ Object

Used internally to make the DSL work. Might be changed at any time to make a better implementation.



128
129
130
# File 'lib/google_static_maps_helper/map.rb', line 128

def marker(*args) # :nodoc:
  self << Marker.new(*args)
end

#markersObject

Returns all the markers which this map holds



79
80
81
# File 'lib/google_static_maps_helper/map.rb', line 79

def markers
  @map_enteties.select {|e| e.is_a? Marker}
end

#path(*args) ⇒ Object

Used internally to make the DSL work. Might be changed at any time



135
136
137
# File 'lib/google_static_maps_helper/map.rb', line 135

def path(*args) # :nodoc:
  self << Path.new(*args)
end

#pathsObject

Returns all the paths which this map holds



99
100
101
# File 'lib/google_static_maps_helper/map.rb', line 99

def paths
  @map_enteties.select {|e| e.is_a? Path}
end

#sizeObject

Returns size as a string, “wxh”



166
167
168
# File 'lib/google_static_maps_helper/map.rb', line 166

def size
  [@width, @height].join('x')
end

#size=(size) ⇒ Object

Sets the size of the map

size

Can be a “wxh”, [w,h] or => x, :height => y



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/google_static_maps_helper/map.rb', line 144

def size=(size)
  unless size.nil?
    case size
    when String
      width, height = size.split('x')
    when Array
      width, height = size
    when Hash
      width = size[:width]
      height = size[:height]
    else
      raise "Don't know how to set size from #{size.class}!"
    end

    self.width = width if width
    self.height = height if height
  end
end

#urlObject

Builds up a URL representing the state of this Map object

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/google_static_maps_helper/map.rb', line 50

def url
  raise BuildDataMissing, "We have to have markers, paths or center and zoom set when url is called!" unless can_build?
  
  out = "#{API_URL}?"

  params = []
  (REQUIRED_OPTIONS + OPTIONAL_OPTIONS).each do |key|
    value = send(key)
    params << "#{key}=#{URI.escape(value.to_s)}" unless value.nil?
  end
  out += params.join('&')

  params = []
  grouped_markers.each_pair do |marker_options_as_url_params, markers|
    markers_locations = markers.map { |m| m.location_to_url }.join('|')
    params << "markers=#{marker_options_as_url_params}|#{markers_locations}"
  end
  out += "&#{params.join('&')}" unless params.empty?
  
  params = []
  paths.each {|path| params << path.url_params}
  out += "&#{params.join('&')}" unless params.empty?

  out
end