Class: Google::Maps::Static::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/google/maps/static/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Map

Returns a new instance of Map.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/google/maps/static/map.rb', line 10

def initialize *args
  @base = "http://maps.google.com/maps/api/staticmap"
  
  @options = {
    # :zoom   => 12,
    :size   => [400, 400],
    :sensor => false
  }
  @options.merge! args.extract_options!
  
  @items = args.select { |item| item.is_a?(Items::Base) }.map(&:to_s)
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/google/maps/static/map.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#<<(item) ⇒ Object Also known as: add_item

Raises:



28
29
30
31
32
# File 'lib/google/maps/static/map.rb', line 28

def << item
  raise GMapper::Error, "Please only use Items::Base decendants." unless item.is_a?(Items::Base)
  @items << item.to_s
  @url = nil if @url # Object has become dirty, url should be regenerated
end

#[]=(key, value) ⇒ Object



23
24
25
26
# File 'lib/google/maps/static/map.rb', line 23

def []= key, value
  @options[key] = value
  @url = nil if @url # Object has become dirty, url should be regenerated
end

#save(file) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/google/maps/static/map.rb', line 35

def save file
  uri = ::URI.parse( url._encode )
  http = ::Net::HTTP.new( uri.host, uri.port )
  http.read_timeout = @timeout if @timeout
  
  response = http.get( "#{uri.path}?#{uri.query}" )
  File.open( file, 'wb' ) { |f| f.write response.body }
end

#urlObject Also known as: to_s



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/google/maps/static/map.rb', line 44

def url
  unless @options.key?(:center) || !@items.empty?
    raise GMapper::Error, "Must specify the center location unless you add one or more map items."
  end
  
  unless @options.key?(:zoom) || !@items.empty?
    raise GMapper::Error, "Must specify the zoom level unless you add one or more map items."
  end
  
  if @url.nil?
    url = "#{@base}?#{parameters}"
    
    def url._encode
      gsub(/\|/) { |match| '%' + match.unpack('H2').join.upcase }
    end
    
    @url = url
  end
  
  @url
end