Class: StaticGmaps::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/static_gmaps.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Map

Returns a new instance of Map.



70
71
72
73
74
75
76
77
# File 'lib/static_gmaps.rb', line 70

def initialize(options = {})
  self.center   = options[:center]
  self.zoom     = options[:zoom]     || StaticGmaps::default_zoom
  self.size     = options[:size]     || StaticGmaps::default_size
  self.map_type = options[:map_type] || StaticGmaps::default_map_type
  self.key      = options[:key]      || StaticGmaps::default_key
  self.markers  = options[:markers]  || [ ]
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def center
  @center
end

#keyObject

Returns the value of attribute key.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def key
  @key
end

#map_typeObject

Returns the value of attribute map_type.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def map_type
  @map_type
end

#markersObject

Returns the value of attribute markers.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def markers
  @markers
end

#sizeObject

Returns the value of attribute size.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def size
  @size
end

#zoomObject

Returns the value of attribute zoom.



63
64
65
# File 'lib/static_gmaps.rb', line 63

def zoom
  @zoom
end

Instance Method Details

#heightObject



83
84
85
# File 'lib/static_gmaps.rb', line 83

def height
  size[1]
end

#markers_url_fragmentObject



116
117
118
119
120
121
122
# File 'lib/static_gmaps.rb', line 116

def markers_url_fragment
  if markers && markers.any?
    return markers.collect{|marker| marker.url_fragment }.join('|')
  else
    return nil
  end
end

#to_blobObject



124
125
126
127
# File 'lib/static_gmaps.rb', line 124

def to_blob
  fetch
  return @blob
end

#urlObject

Raises:

  • (MissingArgument)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/static_gmaps.rb', line 88

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(!self.center && !(markers && markers.size >= 1))
    self.center = StaticGmaps::default_center
  end
  
  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 #{StaticGmaps::maximum_markers} markers." if markers && markers.size > StaticGmaps::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 #{StaticGmaps::maximum_url_size} characters.  Try fewer or less precise markers." if x.size > StaticGmaps::maximum_url_size
  return x
end

#widthObject



79
80
81
# File 'lib/static_gmaps.rb', line 79

def width
  size[0]
end