I’m trying to use the Google Maps API in a Sinatra app. I get a TypeError (can’t convert String into Integer) whenever I look up an address.
Take a look at my code and let me know what you think the problem is.
require 'sinatra'
require 'json'
require 'open-uri'
get '/' do
haml :index
end
post '/' do
find_location(params[:location])
end
def find_location(address)
url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + CGI.escape(address) + "&sensor=false"
resp = open(url).read
parsed_resp = JSON.parse(resp)
lat = parsed_resp['results']['geometry']['location']['lat']
lng = parsed_resp['results']['geometry']['location']['lng']
lat + " " + lng
end
parsed_resp['results']is an array, so your code should look like: