HTTParty Google映射出发地和目的地之间的路线。

时间:2015-02-01 18:58:34

标签: ruby-on-rails ruby json google-maps google-maps-api-3

我的目标:http://maps.googleapis.com/maps/api/directions/json?origin=montreal&destination=toronto&sensor=false

我的课程:

class GoogleMap
  include HTTParty
  base_uri 'http://maps.googleapis.com'

  attr_accessor :origin, :destination

   def initialize(service, page)
    @options = { query: {origin: origin, destination: destination} }
  end

  def directions
    self.class.get("/maps/api/directions/json", @options)
  end
end

目前,当我在控制台上运行时:

irb(main):001:0> g = GoogleMap.new("montreal", "toronto")
=> #<GoogleMap:0x007fcaeeb88538 @options={:query=>{:origin=>nil, :destination=>nil}}>
irb(main):002:0> g.directions
=> #<HTTParty::Response:0x7fcaeeb60b00 parsed_response={"error_message"=>"Invalid request. Missing the 'origin' parameter.", "routes"=>[]...

问题是:{:query=>{:origin=>nil, :destination=>nil}}来源和目的地为零。

我想知道我将如何实现:

irb(main):001:0> g = GoogleMap.new("montreal", "toronto")
=> #<GoogleMap:0x007fcaeeb88538 @options={:query=>{:origin=>montreal, :destination=>toronto}}

然后当我跑:

g.directions我获得了http://maps.googleapis.com/maps/api/directions/json?origin=montreal&destination=toronto&sensor=false

的输出

提前谢谢你。

1 个答案:

答案 0 :(得分:2)

我想你可能想要改变你的

def initialize(service, page)

def initialize(origin, destination)

或者,在致电g.origin = "montreal"之前,您可以g.destination = "toronto"g.directions