我正在尝试按照http://ruby.bastardsbook.com/chapters/methods-and-gems/获取特定位置的纬度和经度
Here is the snippet:
require 'rubygems'
require 'rest-client'
require 'crack'
def get_coordinates_from_address(addr)
base_google_url = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
res = RestClient.get(URI.encode("#{base_google_url}#{addr}"))
parsed_res = Crack::XML.parse(res)
lat = parsed_res["GeocodeResponse"]["result"]["geometry"]["location"]["lat"]
lng = parsed_res["GeocodeResponse"]["result"]["geometry"]["location"]["lng"]
return "#{lat}, #{lng}"
end
latlng = get_coordinates_from_address("1 Times Square, NYC")
puts latlng
我得到以下堆栈跟踪:
/Users/archie/agile/latlang.rb:9:in `[]': can't convert String into Integer (TypeError)
from /Users/archie/agile/latlang.rb:9:in `get_coordinates_from_address'
from /Users/archie/agile/latlang.rb:15:in `<main>'
[Finished]
可能的问题是什么?