Google距离 - 将航点设置为lat,lng

时间:2015-12-14 12:32:55

标签: ruby routes google-direction

我正在使用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)代替地名时,它会返回错误。

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)格式定义航点和起点?

1 个答案:

答案 0 :(得分:1)

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

  1. 您正在使用Google Maps API的JSON API - the documentation is available here
  2. 您显示工作的第一个网址是由fluke工作。根据{{​​3}},

      

    在Waypoints参数中指定航点并由其组成   由管道分隔的一个或多个地址或位置(|)   字符。

    因此,第一个示例所需的查询参数是height: auto;

    有效网址为documentation

  3. 为了使用航点的纬度,经度,您可以使用origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=Malaga

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

  4. 您不能使用嵌套的Ruby Hash作为http://maps.googleapis.com/maps/api/directions/json?origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42的输入,因为它会产生不正确的查询字符串。您需要简化origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42127,如下所示,以便它符合API的要求

    params