class PocketMath::Geocode::PocketMath::Geocode::PocketMath::Geocode::Client

Public Class Methods

new( opts = { :mapquest_api_key => nil, :provider => DEFAULT_GEOCODING_PROVIDER } ) click to toggle source
# File lib/pocketmath-geocode.rb, line 18
def initialize( opts = { :mapquest_api_key => nil, :provider => DEFAULT_GEOCODING_PROVIDER } )
  if !opts[:provider].nil?
    @provider = opts[:provider]
  else 
    @provider = DEFAULT_GEOCODING_PROVIDER
  end

  if !opts[:mapquest_api_key].nil?
    @mapquest_api_key = opts[:mapquest_api_key]
  else
    @mapquest_api_key = nil
  end

  if @provider == :open && @mapquest_api_key.nil?
    raise "Geocoding provider OPEN requires MapQuest API key.  No key was specified.  Please specify like this:  Geocode.open{ :mapquest_api_key => \"...\" }"
  end
end

Public Instance Methods

close() click to toggle source
# File lib/pocketmath-geocode.rb, line 108
def close
end
get_gps_coordinates(locations, max_results = 1) click to toggle source
# File lib/pocketmath-geocode.rb, line 87
def get_gps_coordinates(locations, max_results = 1)
  _locations = nil
  if (!locations.is_a?(Array))
    _locations = [ locations ]
  else
    _locations = locations
  end
  gps_coordinates = []        
  _locations.each do |location|
    if @provider == :google
      gps_coordinates = gps_coordinates + get_gps_coordinates_google(location, max_results)
    elsif @provider == :open
      gps_coordinates = gps_coordinates + get_gps_coordinates_open(location, max_results)
    else
      raise "map provider was not specified"
    end
  end
  gps_coodinates = gps_coordinates.zip
  return gps_coordinates
end

Private Instance Methods

get_gps_coordinates_google(location, max_results = 1) click to toggle source

Use of the Google Map API requires licensing from Google.

# File lib/pocketmath-geocode.rb, line 61
def get_gps_coordinates_google(location, max_results = 1)
  url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{URI.encode(location)}&sensor=true"
  response = Net::HTTP.get_response(URI.parse(url))
  data = response.body
  obj = JSON.parse(data)
  p url
  p obj
  gps_coordinates = []
  n = 0
  obj["results"].each do |result|
    break if n >= max_results
    geometry = result["geometry"]
    raise "geometry was unavailable" if geometry.nil?
    location = geometry["location"]
    raise "location was unavailable" if location.nil?
    lat = location["lat"]
    lng = location["lng"]
    gps_coordinates << { :latitude => lat, :longitude => lng }
    n += 1
  end
  return nil if gps_coordinates.empty?
  return gps_coordinates  
end
get_gps_coordinates_open(location, max_results = 1) click to toggle source
# File lib/pocketmath-geocode.rb, line 38
def get_gps_coordinates_open(location, max_results = 1)
  url = "http://open.mapquestapi.com/geocoding/v1/address?key=#{@mapquest_api_key}&location=#{URI.encode(location)}&maxResults=#{max_results.to_s}&inFormat=kvp&outFormat=json&json=true"
  response = Net::HTTP.get_response(URI.parse(url))
  data = response.body
  obj = JSON.parse(data)
  p url
  p obj
  gps_coordinates = []
  obj["results"].each do |result|
    result["locations"].each do |location|
      lat_lng = location["latLng"]
      gps_coordinates << { :latitude => lat_lng["lat"], :longitude => lat_lng["lng"] }
    end
  end
  return nil if gps_coordinates.empty?
  return gps_coordinates
end