Class: GoogleStaticMapsHelper::Map
- Inherits:
-
Object
- Object
- GoogleStaticMapsHelper::Map
- 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
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#<<(entity) ⇒ Object
Pushes either a Marker or a Path on to the map.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#format=(format) ⇒ Object
Sets the format of the map.
-
#grouped_markers ⇒ Object
Returns the markers grouped by it’s label, color and size.
-
#initialize(options = {}) ⇒ Map
constructor
Creates a new Map object.
-
#key=(key) ⇒ Object
Google static maps does no longer use key when a map is requested, thus this is deprecated.
-
#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.
- #length ⇒ Object
-
#maptype=(type) ⇒ Object
Sets the map type of the map.
-
#marker(*args) ⇒ Object
Used internally to make the DSL work.
-
#markers ⇒ Object
Returns all the markers which this map holds.
-
#path(*args) ⇒ Object
Used internally to make the DSL work.
-
#paths ⇒ Object
Returns all the paths which this map holds.
-
#size ⇒ Object
Returns size as a string, “wxh”.
-
#size=(size) ⇒ Object
Sets the size of the map.
-
#url ⇒ Object
Builds up a URL representing the state of this Map object.
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( = {}) inject_defaults_from_module_class_attribute!() () () .each_pair { |k, v| send("#{k}=", v) } @map_enteties = [] end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
21 22 23 |
# File 'lib/google_static_maps_helper/map.rb', line 21 def height @height end |
#width ⇒ Object
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 |
#each ⇒ Object
112 113 114 |
# File 'lib/google_static_maps_helper/map.rb', line 112 def each @map_enteties.each {|m| yield(m)} end |
#empty? ⇒ 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.
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_markers ⇒ Object
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.] << 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 |
#length ⇒ Object
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.
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 |
#markers ⇒ Object
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 |
#paths ⇒ Object
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 |
#size ⇒ Object
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 |
#url ⇒ Object
Builds up a URL representing the state of this Map object
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 |, markers| markers_locations = markers.map { |m| m.location_to_url }.join('|') params << "markers=#{}|#{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 |