【问题标题】:Google Distance - Set waypoints as lat,lng谷歌距离 - 将航点设置为 lat,lng
【发布时间】:2015-12-14 12:32:55
【问题描述】:

我正在使用来自 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) 格式定义航点作为起点和终点?

【问题讨论】:

    标签: ruby routes google-directions-api


    【解决方案1】:

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

    1. 您正在使用 Google Maps API 的 JSON API - the documentation is available here
    2. 您显示为工作的第一个 URL 是由 Fluke 工作的。根据documentation

      航点在航点参数中指定,包括 用竖线 (|) 分隔的一个或多个地址或位置 字符。

      因此,第一个示例所需的查询参数是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

    3. 要使用纬度、经度作为航点,您可以使用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" }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2017-09-06
      • 2018-05-20
      相关资源
      最近更新 更多