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
Instance Attribute Details
#config ⇒ Object (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.
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_location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response
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 |