Class: StaticGmaps::Map
- Inherits:
-
Object
- Object
- StaticGmaps::Map
- Defined in:
- lib/static_gmaps.rb
Constant Summary collapse
- MAXIMUM_URL_SIZE =
1978
- MAXIMUM_MARKERS =
50
- DEFAULT_CENTER =
[ 0, 0 ]
- DEFAULT_ZOOM =
1
- DEFAULT_SIZE =
[ 500, 400 ]
- DEFAULT_MAP_TYPE =
:roadmap
- DEFAULT_KEY =
'ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA'
- DEFAULT_MARKERS =
[ ]
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#key ⇒ Object
Returns the value of attribute key.
-
#map_type ⇒ Object
Returns the value of attribute map_type.
-
#markers ⇒ Object
Returns the value of attribute markers.
-
#size ⇒ Object
Returns the value of attribute size.
-
#zoom ⇒ Object
Returns the value of attribute zoom.
Instance Method Summary collapse
- #height ⇒ Object
-
#initialize(options = {}) ⇒ Map
constructor
A new instance of Map.
- #markers_url_fragment ⇒ Object
- #to_blob ⇒ Object
- #url ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Map
Returns a new instance of Map.
45 46 47 48 49 50 51 52 |
# File 'lib/static_gmaps.rb', line 45 def initialize( = {}) self.center = [:center] || DEFAULT_CENTER self.zoom = [:zoom] || DEFAULT_ZOOM self.size = [:size] || DEFAULT_SIZE self.map_type = [:map_type] || DEFAULT_MAP_TYPE self.key = [:key] || DEFAULT_KEY self.markers = [:markers] || DEFAULT_MARKERS end |
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def center @center end |
#key ⇒ Object
Returns the value of attribute key.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def key @key end |
#map_type ⇒ Object
Returns the value of attribute map_type.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def map_type @map_type end |
#markers ⇒ Object
Returns the value of attribute markers.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def markers @markers end |
#size ⇒ Object
Returns the value of attribute size.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def size @size end |
#zoom ⇒ Object
Returns the value of attribute zoom.
38 39 40 |
# File 'lib/static_gmaps.rb', line 38 def zoom @zoom end |
Instance Method Details
#height ⇒ Object
58 59 60 |
# File 'lib/static_gmaps.rb', line 58 def height size[1] end |
#markers_url_fragment ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/static_gmaps.rb', line 86 def markers_url_fragment if markers && markers.any? return markers.collect{|marker| marker.url_fragment }.join('|') else return nil end end |
#to_blob ⇒ Object
94 95 96 97 |
# File 'lib/static_gmaps.rb', line 94 def to_blob fetch return @blob end |
#url ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/static_gmaps.rb', line 63 def url raise MissingArgument.new("Size must be set before a url can be generated for Map.") if !size || !size[0] || !size[1] raise MissingArgument.new("Key must be set before a url can be generated for Map.") if !key 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 #{MAXIMUM_MARKERS} markers." if markers && markers.size > MAXIMUM_MARKERS parameters = {} parameters[:size] = "#{size[0]}x#{size[1]}" parameters[:key] = "#{key}" 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 = 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/staticmap?#{parameters}" raise "Google doesn't like the url to be longer than #{MAXIMUM_URL_SIZE} characters. Try fewer or less precise markers." if x.size > MAXIMUM_URL_SIZE return x end |
#width ⇒ Object
54 55 56 |
# File 'lib/static_gmaps.rb', line 54 def width size[0] end |