Class: StaticMap::Image

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options={})
  @markers  = options[:markers] || []
  @size     = options[:size]    || '500x500'
  @sensor   = options[:sensor]  || true
  @zoom     = options[:zoom]    || 1
  @center   = options[:center]  || nil
  @maptype  = options[:maptype] || 'road'
  @path     = options[:path]    || nil
  @alt      = options[:alt]     || nil
  @title    = options[:title]   || nil
end

Instance Attribute Details

#altObject

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

#centerObject

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

#maptypeObject

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

#markersObject

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

#pathObject

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

#sensorObject

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

#sizeObject

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

#titleObject

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

#zoomObject

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

#fileObject



48
49
50
# File 'lib/static_map.rb', line 48

def file
  open(url)
end

#marker_paramsObject



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

#paramsObject



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

#saveObject



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_htmlObject 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

#urlObject



52
53
54
# File 'lib/static_map.rb', line 52

def url
  "#{URL}?#{params}".strip
end