Skip to content

Instantly share code, notes, and snippets.

@ryanatwork
Created September 28, 2011 04:19
Show Gist options
  • Save ryanatwork/1246973 to your computer and use it in GitHub Desktop.
Save ryanatwork/1246973 to your computer and use it in GitHub Desktop.
Closest one
require 'sinatra'
require 'json'
require 'net/http'
require 'haml'
require 'geocoder'
get '/' do
b = JSON.parse(Net::HTTP.get(URI.parse("http://data.cityofchicago.org/api/views/nen3-vcxj/rows.json")))
us = [41.864447,-87.644806]
a = b["data"].min_by {|x| dist(x,us)}
"#{a}"
end
get '/address/:location' do
b = JSON.parse(Net::HTTP.get(URI.parse("http://data.cityofchicago.org/api/views/nen3-vcxj/rows.json")))
location = params[:location]
us = Geocoder.coordinates(location)
a = b["data"].min_by {|x| dist(x,us)}
"#{a[1]}"
end
def dist(entry,loc)
entry.last[1..2].map(&:to_f).zip(loc).inject(0) {|s,(c1,c2)| s+(c1-c2)**2}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment