Class: GmapsGeocoding::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/gmaps_geocoding/api.rb

Overview

Google Maps Geocoding Service abstraction class

Examples:

opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Api

Returns a new instance of Api.



13
14
15
# File 'lib/gmaps_geocoding/api.rb', line 13

def initialize(opts = {})
  @config = Config.new(opts)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/gmaps_geocoding/api.rb', line 11

def config
  @config
end

Instance Method Details

#get_finest_latlng(data_result) ⇒ Array

Get the best latlng for an address based on Google Maps Geocoder “location_type”

location_type stores additional data about the specified location. The following values are currently supported:

google.maps.GeocoderLocationType.ROOFTOP            indicates that the returned result reflects a precise geocode.
google.maps.GeocoderLocationType.RANGE_INTERPOLATED indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
google.maps.GeocoderLocationType.GEOMETRIC_CENTER   indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).
google.maps.GeocoderLocationType.APPROXIMATE        indicates that the returned result is approximate.

Examples:

# json output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)
data = api.get_location
if data.include?('status') && data['status'].eql?('OK') # or more simple : if data.include?('results')
  return get_finest_latlng(data['results']) # output : [2.291018, 48.857269]
end

Parameters:

  • data_result (Array)

    The json#results or xml#result array from #get_location method

Returns:

  • (Array)

    array contains latitude and longitude of the location



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gmaps_geocoding/api.rb', line 68

def get_finest_latlng(data_result)
  tmp_result = {}
  data = data_result
  data.each do |d|
    tmp_result["#{d['geometry']['location_type']}"] = {lng: d['geometry']['location']['lng'].to_f,
                                                   lat: d['geometry']['location']['lat'].to_f}
  end
  if tmp_result.include?('ROOFTOP')
    [tmp_result['ROOFTOP'][:lng], tmp_result['ROOFTOP'][:lat]]
  elsif tmp_result.include?('RANGE_INTERPOLATED')
    [tmp_result['RANGE_INTERPOLATED'][:lng], tmp_result['RANGE_INTERPOLATED'][:lat]]
  elsif tmp_result.include?('GEOMETRIC_CENTER')
    [tmp_result['GEOMETRIC_CENTER'][:lng], tmp_result['GEOMETRIC_CENTER'][:lat]]
  else
    [tmp_result['APPROXIMATE'][:lng], tmp_result['APPROXIMATE'][:lat]]
  end
end

#get_locationObject

Return a Ruby Hash object of the Google Maps Geocoding Service response

/ Google Maps Geocoding Service documentation.

Examples:

# json output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)
result = api.get_location
# xml output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'xml'}
api = GmapsGeocoding::Api.new(opts)
result = api.get_location


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gmaps_geocoding/api.rb', line 31

def get_location
  begin
    if @config.valid?
      rest_client = retrieve_geocoding_data
      result = case @config.is_json_format?
                 when true
                   GmapsGeocoding.from_json(rest_client.to_s)
                 else
                   GmapsGeocoding.from_xml(rest_client.to_s)
               end
      return result
    end
  rescue => e
    puts "[error: gmaps_geocoding]: #{e}"
  end
  nil
end