Class: Mapbox::Geocoder

Inherits:
Object
  • Object
show all
Extended by:
HashUtils
Includes:
APIOperations::Request
Defined in:
lib/mapbox/geocoder.rb

Class Method Summary collapse

Methods included from HashUtils

xy_from_hash

Methods included from APIOperations::Request

included

Class Method Details

.assemble_params(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mapbox/geocoder.rb', line 10

def self.assemble_params(options={})
  proximity = options[:proximity]
  bbox = options[:bbox]
  types = options[:types]
  params = ''
  opts = options.select { |key, value| key != :proximity && key != :bbox && key != :types}
  if opts.length > 0
    params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
  end

  if proximity then
    lon = proximity[:longitude].round(3)
    lat = proximity[:latitude].round(3)
    params += "#{params.length > 0 ? '&' : '?'}proximity=#{lon}%2C#{lat}"
  end

  if bbox then
    params += "#{params.length > 0 ? '&' : '?'}bbox=#{bbox.join('%2C')}"
  end

  if types then
    params += "#{params.length > 0 ? '&' : '?'}types=#{types.join('%2C')}"
  end

  return params
end

.geocode_forward(query, options = {}, dataset = 'mapbox.places') ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/mapbox/geocoder.rb', line 37

def self.geocode_forward(query, options={}, dataset='mapbox.places')
  params = self.assemble_params(options)

  return request(
    :get,
    "/geocoding/v5/#{dataset}/#{URI.encode_www_form_component(query)}.json#{params}",
    nil)
end

.geocode_reverse(location, options = {}, dataset = 'mapbox.places') ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mapbox/geocoder.rb', line 46

def self.geocode_reverse(location, options={}, dataset='mapbox.places')
  location[:longitude] = location[:longitude].round(5)
  location[:latitude] = location[:latitude].round(5)

  params = self.assemble_params(options)

  return request(
    :get,
    "/geocoding/v5/#{dataset}/#{xy_from_hash(location).join(',')}.json#{params}",
    nil)
end