Class: GoogleMapStaticImage

Inherits:
Object
  • Object
show all
Defined in:
lib/google_map_static_image.rb,
lib/google_map_static_image/version.rb

Defined Under Namespace

Classes: ApiError

Constant Summary collapse

STATIC_MAP_URL =
"https://maps.googleapis.com/maps/api/staticmap"
VALID_MAP_TYPES =
%w[roadmap satellite terrain hybrid].freeze
VERSION =
"0.0.4"

Instance Method Summary collapse

Instance Method Details

#get_response(api_key, markers, option = {}) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google_map_static_image.rb', line 17

def get_response(api_key, markers, option = {})
  query = {}
  query[:key]     = api_key
  query[:size]    = "1024x1024"
  query[:scale]   = 2
  query[:center]  = option[:center]                                          unless option[:center].nil?
  query[:zoom]    = option[:zoom]                                            unless option[:zoom].nil?
  query[:maptype] = option[:map_type] if VALID_MAP_TYPES.include?(option[:map_type].to_s)
  query[:style]   = option[:style].map { |k, v| "#{k}:#{v}" }.join("|")    unless option[:style].nil?
  query[:path]    = option[:path].map { |k, v| "#{k}:#{v}" }.join("|")     unless option[:path].nil?
  unless option[:location].nil?
    query[:path] = [query[:path], option[:location].map { |_k, v| v.to_s }.join("|")].join("|")
  end
  query[:markers] = option[:markers].map { |val| val.join("|") }            unless option[:markers].nil?

  response = HTTParty.get(STATIC_MAP_URL, query: query)
  raise ApiError.new(response.code, response.body) unless response.code == 200
  response.parsed_response
end