Class: StaticMap::Image
- Inherits:
-
Object
- Object
- StaticMap::Image
- Defined in:
- lib/static_map.rb
Overview
Example
img = StaticMap::Image.new(size: 500) img.center = ‘Burlington, VT’ img.zoom = 4 img.markers << 123, longitude: 321, color: ‘red’, label: ‘VT’
Constant Summary collapse
- URL =
Base URL for Google Static Maps API endpoint
"http://maps.google.com/maps/api/staticmap"
Instance Attribute Summary collapse
-
#alt ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#center ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#maptype ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#markers ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#path ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#sensor ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#size ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#title ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
-
#zoom ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image.
Instance Method Summary collapse
- #file ⇒ Object
-
#initialize(options = {}) ⇒ Image
constructor
A new instance of Image.
- #marker_params ⇒ Object
- #params ⇒ Object
- #save ⇒ Object
- #to_html ⇒ Object (also: #to_s)
- #url ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Image
Returns a new instance of Image.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/static_map.rb', line 30 def initialize(={}) @markers = [:markers] || [] @size = [:size] || '500x500' @sensor = [:sensor] || true @zoom = [:zoom] || 1 @center = [:center] || nil @maptype = [:maptype] || 'road' @path = [:path] || nil @alt = [:alt] || nil @title = [:title] || nil end |
Instance Attribute Details
#alt ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def alt @alt end |
#center ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def center @center end |
#maptype ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def maptype @maptype end |
#markers ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def markers @markers end |
#path ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def path @path end |
#sensor ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def sensor @sensor end |
#size ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def size @size end |
#title ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def title @title end |
#zoom ⇒ Object
center String - center the map around this location zoom Integer - zoom level of the map size String - in pixels of the image. 500x500 sensor Boolean - autodetect user location markers Array of Hashes - location of pins on map. Requires a location address or lat/long coordinates maptype String - type of map (satellite, road, hybrid, terrain) path String - path to write file to when #save is called alt String - alt text if using image tag title String - title text if using image tag
28 29 30 |
# File 'lib/static_map.rb', line 28 def zoom @zoom end |
Instance Method Details
#file ⇒ Object
48 49 50 |
# File 'lib/static_map.rb', line 48 def file open(url) end |
#marker_params ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/static_map.rb', line 65 def marker_params @markers.map do |marker| str = ["markers="] str << [CGI.escape("color:#{marker[:color]}")] if marker[:color] str << [CGI.escape("label:#{marker[:label]}")] if marker[:label] str << [CGI.escape("#{marker[:location]}")] if marker[:location] str << ["#{marker[:latitude]},#{marker[:longitude]}"] if marker[:latitude] && marker[:longitude] str.map{|v| v }.join("%7C") # %7C is | character end.join("&").gsub(/\=\%7C/i, '=') end |
#params ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/static_map.rb', line 56 def params x = { size: size, center: center, zoom: zoom, sensor: sensor, maptype: maptype }.reject { |k,v| v.nil? }.map do |k,v| "#{k}=#{CGI.escape(v.to_s)}" end.join("&") x += "&#{marker_params}" if @markers.any? x end |
#save ⇒ Object
42 43 44 45 46 |
# File 'lib/static_map.rb', line 42 def save raise "Please set the path argument to save the image" unless @path File.open(@path, "w") { |f| f.write(file.read) } end |
#to_html ⇒ Object Also known as: to_s
76 77 78 |
# File 'lib/static_map.rb', line 76 def to_html "<img src='#{url}' title='#{title}' alt='#{alt}'/>" end |
#url ⇒ Object
52 53 54 |
# File 'lib/static_map.rb', line 52 def url "#{URL}?#{params}".strip end |