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.

Returns:

  • (Symbol)

    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.

Parameters:

  • size (Hash)

    The rectangular dimensions of the map image.

  • center (String, Hash) (defaults to: nil)

    The address or lat/lng value of the map’s center.

  • zoom (Integer) (defaults to: nil)

    The magnification level of the map.

  • scale (Integer) (defaults to: 1)

    The scale of the map. This affects the number of pixels that are returned.

  • format (String) (defaults to: "png")

    The format of the resulting image. Defaults to “png8” or “png”.

  • maptype (String) (defaults to: "roadmap")

    The type of map to construct. Defaults to “roadmap”.

  • language (String) (defaults to: nil)

    The language to use for display of labels on map tiles.

  • region (String) (defaults to: nil)

    The region code specified as a two-character ccTLD (‘top-level domain’) value.

  • markers (String, Array) (defaults to: nil)

    One or more markers to attach to the image at specified locations.

  • path (String, Array) (defaults to: nil)

    The single path of two or more connected points to overlay in the image at the specified locations.

  • visible (String, Array) (defaults to: nil)

    One or more locations that should remain visible on the map.

  • style (String) (defaults to: nil)

    A custom style to alter the presentation of a specific feature (roads, parks, and other features) of the map.

Returns:

  • (Hash)

    Hash with image URL, MIME type and its base64-encoded value.

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