class GoogleStaticMapsHelper::Map
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.
Constants
- MAX_HEIGHT
- MAX_WIDTH
- OPTIONAL_OPTIONS
- REQUIRED_OPTIONS
- VALID_FORMATS
- VALID_MAP_TYPES
Attributes
Public Class Methods
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 {:width => 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
# File lib/google_static_maps_helper/map.rb, line 37 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
Public Instance Methods
# File lib/google_static_maps_helper/map.rb, line 111 def each @map_enteties.each {|m| yield(m)} end
# File lib/google_static_maps_helper/map.rb, line 115 def empty? @map_enteties.empty? end
Sets the format of the map
format
-
Can be any values included in
VALID_FORMATS
.
# File lib/google_static_maps_helper/map.rb, line 189 def format=(format) @format = format.to_s raise UnsupportedFormat unless VALID_FORMATS.include? @format end
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.
# File lib/google_static_maps_helper/map.rb, line 88 def grouped_markers markers.inject(Hash.new {|hash, key| hash[key] = []}) do |groups, marker| groups[marker.options_to_url_params] << marker groups end end
# File lib/google_static_maps_helper/map.rb, line 119 def length @map_enteties.length end
Sets the map type of the map
type
-
Can be any values included in
VALID_MAP_TYPES
.
# File lib/google_static_maps_helper/map.rb, line 199 def maptype=(type) @maptype = type.to_s raise UnsupportedMaptype unless VALID_MAP_TYPES.include? @maptype end
Returns all the markers which this map holds
# File lib/google_static_maps_helper/map.rb, line 78 def markers @map_enteties.select {|e| e.is_a? Marker} end
Returns all the paths which this map holds
# File lib/google_static_maps_helper/map.rb, line 98 def paths @map_enteties.select {|e| e.is_a? Path} end
Returns size as a string, “wxh”
# File lib/google_static_maps_helper/map.rb, line 165 def size [@width, @height].join('x') end
Sets the size of the map
size
-
Can be a “wxh”, [w,h] or {:width => x, :height => y}
# File lib/google_static_maps_helper/map.rb, line 143 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
Builds up a URL representing the state of this Map
object
# File lib/google_static_maps_helper/map.rb, line 49 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::DEFAULT_PARSER.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
Private Instance Methods
Returns an answer for if we can build the URL or not
# File lib/google_static_maps_helper/map.rb, line 229 def can_build? !@map_enteties.empty? || (center && zoom) end
# File lib/google_static_maps_helper/map.rb, line 243 def inject_defaults_from_module_class_attribute!(options) REQUIRED_OPTIONS.each do |option_key| next if options.has_key? option_key value_from_modul = GoogleStaticMapsHelper.send(option_key) options[option_key] = value_from_modul unless value_from_modul.nil? end end
# File lib/google_static_maps_helper/map.rb, line 238 def validate_options(options) invalid_options = options.keys - REQUIRED_OPTIONS - OPTIONAL_OPTIONS raise OptionNotExist, "The following options does not exist: #{invalid_options.join(', ')}" unless invalid_options.empty? end
# File lib/google_static_maps_helper/map.rb, line 233 def validate_required_options(options) missing_options = REQUIRED_OPTIONS - options.keys raise OptionMissing, "The following required options are missing: #{missing_options.join(', ')}" unless missing_options.empty? end