Class: GmapsGeocoding::Api
- Inherits:
-
Object
- Object
- GmapsGeocoding::Api
- Defined in:
- lib/gmaps_geocoding/api.rb
Overview
Google Maps Geocoding Service abstraction class
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#get_finest_latlng(data_result) ⇒ Array
Get the best latlng for an address based on Google Maps Geocoder “location_type”.
-
#get_location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response.
-
#initialize(opts = {}) ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize(opts = {}) ⇒ Api
Returns a new instance of Api.
14 15 16 17 18 19 20 |
# File 'lib/gmaps_geocoding/api.rb', line 14 def initialize(opts = {}) @logger = opts.delete(:logger)||Logger.new(STDERR) do |l| l.progname = 'gmaps_geocoding' l.level = $DEBUG ? Logger::DEBUG : Logger::INFO end @config = GmapsGeocoding::Config.new(opts) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/gmaps_geocoding/api.rb', line 12 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gmaps_geocoding/api.rb', line 73 def get_finest_latlng(data_result) tmp_result = {} data = data_result if data.kind_of?(Array) 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 else tmp_result["#{data['geometry']['location_type']}"] = { lng: data['geometry']['location']['lng'].to_f, lat: data['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_location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gmaps_geocoding/api.rb', line 36 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 @logger.error e.to_s end nil end |