Class: GoogleMaps::Services::StaticMap

Inherits:
Object
  • Object
show all
Defined in:
lib/googlemaps/services/staticmap.rb

Overview

Performs requests to the Google Static Map API.

Examples:

Get static map image

staticmap = GoogleMaps::Services::StaticMap.new(client)
map_img = staticmap.query(size: {:length => 640, :width => 400},
                          center: "50.8449925,4.362961",
                          maptype: "hybrid",
                          zoom: 16)
# {
#     :url => "https://maps.googleapis.com/maps/api/staticmap?size=640x400&center=50.8449925%2C4.362961&zoom=16&maptype=hybrid",
#     :mime_type => "image/png",
#     :image_data => "iVBORw0KGgoAAAANSUhEUgAAAoAAAAGQCAMAAAAJLSEXAAADAFBMVEU..."
# }

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ StaticMap

Returns a new instance of StaticMap.

Since:

  • 1.0.0



25
26
27
# File 'lib/googlemaps/services/staticmap.rb', line 25

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientSymbol

Returns The HTTP client.

Since:

  • 1.0.0



23
24
25
# File 'lib/googlemaps/services/staticmap.rb', line 23

def client
  @client
end

Instance Method Details

#query(size:, center: nil, zoom: nil, scale: 1, format: "png", maptype: "roadmap", language: nil, region: nil, markers: nil, path: nil, visible: nil, style: nil) ⇒ Hash

Get the static map image.

Since:

  • 1.0.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/googlemaps/services/staticmap.rb', line 45

def query(size:, center: nil, zoom: nil, scale: 1, format: "png", maptype: "roadmap",
          language: nil, region: nil, markers: nil, path: nil, visible: nil, style: nil)
  params = { 'size' => Convert.rectangular_dimensions(size) }

  if markers
    params['markers'] = markers
  else
    raise StandardError, "both center and zoom are required if markers not present." unless (center && zoom)

    params['center'] = Convert.to_latlng(center)
    params['zoom'] = zoom
  end

  if scale != 1
    raise StandardError, "invalid scale value." unless Constants::ALLOWED_SCALES.include? scale
    params['scale'] = scale
  end

  if format != "png"
    raise StandardError, "invalid image format." unless Constants::SUPPORTED_IMG_FORMATS.include? format
    params['format'] = format
  end

  if maptype != "roadmap"
    raise StandardError, "invalid maptype value." unless Constants::SUPPORTED_MAP_TYPES.include? maptype
    params['maptype'] = maptype
  end

  if language
    params['language'] = language
  end

  if region
    params['region'] = region
  end

  if path
    params['path'] = path
  end

  if visible
    params['visible'] = visible
  end

  if style
    params['style'] = style
  end

  self.client.request(url: "/maps/api/staticmap", params: params)
end