This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
/
static_map.rb
62 lines (53 loc) · 1.73 KB
/
static_map.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module BHM
module GoogleMaps
class StaticMap
URL_TEMPLATE = "http://maps.google.com/maps/api/staticmap?%s"
COLOURS = %w(red white green brown blue orange gray purple yellow black)
LABELS = ('A'..'Z').to_a
def initialize(options = {})
@addresses = []
@width, @height = options.fetch(:width, 540), options.fetch(:height, 400)
@params = Hash.new.tap do |p|
p[:sensor] = false
p[:size] = "#{@width}x#{@height}"
p[:maptype] = options.fetch(:type, "roadmap")
p[:zoom] = options.fetch(:zoom, 15) if options[:zoom] != nil
end
end
def <<(address)
@addresses << address
end
def to_url
params = @params.to_param
params << "&"
params << build_marker_params
(URL_TEMPLATE % params).html_safe
end
def self.for_address(address, opts = {})
map = self.new(opts.reverse_merge(:zoom => 15))
map << address
map.to_url
end
def self.for_addresses(*addresses)
options = addresses.extract_options!.reverse_merge(:zoom => nil)
map = self.new(options)
addresses.flatten.each { |a| map << a }
map.to_url
end
protected
def build_marker_params
params = []
@addresses.each_with_index do |address, index|
return "markers=#{to_ll @addresses.first}" if @addresses.size == 1
color = COLOURS[index % COLOURS.size]
label = LABELS[index % LABELS.size]
params << "markers=color:#{color}|label:#{label}|#{to_ll(address)}"
end
params.join("&")
end
def to_ll(address)
"#{address.lat},#{address.lng}"
end
end
end
end