Google 距离 - 将 waypoints 设置为纬度、经度

Google Distance - Set waypoints as lat,lng

我正在使用 Ruby 的 Google Direction Service

我有这个对象:

params = {
  origin: "37.88818,-4.77938",
  destination: "36.51903,-6.28014",
  waypoints: [
    {
      location:"Malaga",
      stopover:false
    }]
}

当我生成 url 时一切正常:

uri = URI('http://maps.googleapis.com/maps/api/directions/json')
uri.query = URI.encode_www_form(params)

http://maps.googleapis.com/maps/api/directions/json?origin=37.88818%2C-4.77938&destination=36.51903%2C-6.28014&waypoints={%3Alocation%3D%3E%22Malaga%22}

但是当我使用 (lat,lng) 而不是将其命名时 returns 错误。

params = {
  origin: "37.88818,-4.77938",
  destination: "36.51903,-6.28014",
  waypoints: [
    {
      location:"36.72126,-4.42127",
      stopover:false
    }]
}

http://maps.googleapis.com/maps/api/directions/json?origin=37.88818%2C-4.77938&destination=36.51903%2C-6.28014&waypoints={%3Alocation%3D%3E%2236.72126%2C-4.42127%22}

如何使用 (lat,lng) 格式定义 waypoints 作为出发地和目的地?

根据我对API的理解,以下是我的观察

  1. 您正在使用 JSON API 个 Google 地图 API - the documentation is available here.
  2. 您显示的第一个URL工作是侥幸。根据 documentation

    Waypoints are specified within the waypoints parameter and consist of one or more addresses or locations separated by the pipe (|) character.

    因此,第一个示例所需的查询参数是 origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=Malaga

    有效的URL将是http://maps.googleapis.com/maps/api/directions/json?origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=Malaga

  3. 为了使用纬度,经度为waypoints,可以使用origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42127

    有效的URL将是http://maps.googleapis.com/maps/api/directions/json?origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42

  4. 您不能使用嵌套的 Ruby 哈希作为 URI#encode_www_form 的输入,因为它会产生不正确的查询字符串。您需要如下所示简化您的 params,使其符合 API

    的要求

    params = { origin: "37.88818,-4.77938", destination: "36.51903,-6.28014", waypoints:"36.72126,-4.42" }