Class: GoogleMaps::Services::Elevation

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

Overview

Performs requests to the Google Maps Elevation API.

Examples:

elevation = GoogleMaps::Services::Elevation.new(client)
result = elevation.query(locations: [{:lat => 52.520645, :lng => 13.409779}, "Brussels"])

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Elevation

Returns a new instance of Elevation.

Since:

  • 1.0.0



16
17
18
# File 'lib/googlemaps/services/elevation.rb', line 16

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientSymbol

Returns The HTTP client.

Returns:

  • (Symbol)

    The HTTP client.

Since:

  • 1.0.0



14
15
16
# File 'lib/googlemaps/services/elevation.rb', line 14

def client
  @client
end

Instance Method Details

#query(locations: nil, path: nil, samples: 0) ⇒ Array, Nokogiri::XML::NodeSet

Provides elevation data for locations provided on the surface of the earth, including depth locations on the ocean floor (which return negative values). Provides elevation data sampled along a path on the surface of the earth.

Parameters:

  • locations (Array) (defaults to: nil)

    Array of lat/lng values from which to calculate elevation data.

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

    An encoded polyline string, or an Array of lat/lng values from which to calculate elevation data.

  • samples (Integer) (defaults to: 0)

    The number of sample points along a path for which to return elevation data.

Returns:

  • (Array, Nokogiri::XML::NodeSet)

    Valid JSON or XML response.

Since:

  • 1.0.0



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
# File 'lib/googlemaps/services/elevation.rb', line 29

def query(locations: nil, path: nil, samples: 0)
  params = {}

  if locations
    params['locations'] = Convert.shortest_path(locations)
  end

  if path
    if path.instance_of? String
      path = "enc:#{path}"
    elsif path.instance_of? Array
      path = Convert.shortest_path(path)
    else
      raise TypeError, 'Path should be either a String or an Array.'
    end

    params = {'path' => path, 'samples' => samples }
  end

  if path && locations
    raise StandardError, 'Should not specify both path and locations.'
  end

  case self.client.response_format
  when :xml
    self.client.request(url: '/maps/api/elevation/xml', params: params).xpath('//result')
  when :json
    self.client.request(url: '/maps/api/elevation/json', params: params)['results']
  else
    raise StandardError, 'Unsupported response format. Should be either :json or :xml.'
  end
end