Class: CloudMade::GeocodingService

Inherits:
Service
  • Object
show all
Defined in:
lib/cloudmade/geocoding.rb

Overview

Class that is responsible for geocoding services of Cloudmade

Instance Attribute Summary

Attributes inherited from Service

#connection, #subdomain

Instance Method Summary collapse

Methods inherited from Service

to_url_params, #url

Instance Method Details

#find(query, options = {}) ⇒ Object

Find objects that match given query. Returns GeoResults object.

  • query Query by which objects should be searched

  • options Additional options, contains

results: How many results should be returned. skip: Number of results to skip from beginning. bbox: Bounding box in which objects should be searched. bbox_only: If set to False, results will be searched in the whole world if there are no results for a given bbox. return_geometry: If specified, adds geometry in returned results. Defaults to true.



33
34
35
36
37
38
39
40
41
# File 'lib/cloudmade/geocoding.rb', line 33

def find(query, options = {})
  options['results'] = 10 unless options.has_key? 'results'
  options['skip'] = 0 unless options.has_key? 'skip'
  options['bbox_only'] = true unless options.has_key? 'bbox_only'
  options['return_geometry'] = true unless options.has_key? 'return_geometry'
  request = "/find/#{CGI.escape(query)}.js?#{Service.to_url_params(options)}"
    
  GeoResults.new(JSON.parse(connect request))
end

#find_closest(object_type, lat, lon, options = {}) ⇒ Object

TODO: Modify lat, lon parameters to point

Raises:



55
56
57
58
59
60
61
# File 'lib/cloudmade/geocoding.rb', line 55

def find_closest(object_type, lat, lon, options = {})
  lat_lon = "#{CGI.escape(lat.to_s + '+' + lon.to_s)}"
  request = "/closest/#{object_type}/#{lat_lon}.js?#{Service.to_url_params(options)}"
  geo_results = GeoResults.new(JSON.parse(connect request))
  raise ObjectNotFound.new if (geo_results.results == nil or geo_results.results.size == 0)
  return geo_results.results[0]
end

#url_templateObject

:nodoc:



64
65
66
# File 'lib/cloudmade/geocoding.rb', line 64

def url_template
  return "http://#{@subdomain}.#{@connection.url}/#{@connection.api_key}/geocoding"
end