【问题标题】:Using Google Maps API to get travel time data [closed]使用 Google Maps API 获取旅行时间数据 [关闭]
【发布时间】:2010-11-05 18:52:01
【问题描述】:

我遇到的所有使用 Google Maps API 的示例似乎都显示了某种地图。当您要求从 A 到 B 的道路描述时,我想将有关估计的汽车旅行时间的数据合并到站点中。而且只有那个数据。是否可以不为最终访问者加载地图?

【问题讨论】:

    标签: api google-maps travel-time


    【解决方案1】:
    1. 首先到这里: https://developers.google.com/maps/documentation/distance-matrix/start 您需要:创建或选择项目、启用 API 和获取 API 密钥 只需单击“获取密钥”并假设您有一个 Gmail 帐户,您就可以开始一切(或者登录到您的 gmail 帐户后转到 console.developer.google.com,然后创建一个应用程序,并启用 API,并在那里获取 API 密钥)。
    2. 现在转到https://developers.google.com/maps/documentation/distance-matrix/intro 并关注详细信息。需要注意的几件事:使用这些: &mode=driving&departure_time=now&traffic_model=pessimistic 如果您希望当前交通影响您的驾驶时间。我也用过 &units=imperial

    仅当满足以下所有条件时才返回流量持续时间:

    请求包含一个离开时间参数。 该请求包括有效的 API 密钥或有效的 Google Maps APIs Premium Plan 客户端 ID 和签名。 交通状况可用于请求的路线。 模式参数设置为驱动。

    至少从 2019 年 5 月开始,您现在可以在有或没有地图的情况下显示行驶时间。

    您可以在 Google 地图上显示距离矩阵 API 结果,也可以不使用地图。如果您想在地图上显示距离矩阵 API 结果,那么这些结果必须显示在 Google 地图上。禁止在非 Google 地图的地图上使用距离矩阵 API 数据。

    来源:Distance Matrix usage limits,另请查看Google Maps Terms & Conditions

    请注意,Mapquest 不会根据实际交通情况显示行驶时间。

    【讨论】:

    【解决方案2】:

    我为此苦苦挣扎了很长时间,但最终通过 AJAX 调用解决了它(确保您已链接到 jQuery 来执行此操作)。

     //Pass parameters to a function so your user can choose their travel 
     locations
    
     function getCommuteTime(departLocation, arriveLocation) {
       //Put your API call here with the parameters passed into it
       var queryURL = 
       "https://maps.googleapis.com/maps/api/distancematrix/json?
       units=imperial&origins=" + departLocation + "&destinations=" + 
       arriveLocation + "&key=YOUR_API_KEY"
    
      //Ajax retrieves data from the API
      $.ajax({
      url: queryURL,
      method: "GET"
      }).then(function(response) {
    
        //Navigate through the given object to the data you want (in this 
         case, commute time)
        var commuteTime = response.rows[0].elements[0].duration.text;
    
        console.log(commuteTime)
       });
    
      } 
    
    
    //This is where your user can give you the data you need
    $("#addRoute").on("click", function(event) {
    
    event.preventDefault();
    
    var departLocation = $("#departInput").val().trim();
    var arriveLocation = $("#arriveInput").val().trim();
    
    //call the above function
    getCommuteTime(departLocation, arriveLocation);
      });
    

    【讨论】:

      【解决方案3】:

      上述答案Google API terms of service违反,因此,不正确。

      Google Maps API 仅允许您在参考显示给用户的 Google 地图时计算行程时间。

      如果您不向服务的最终用户显示 Google 地图,则无法使用 API。

      更新:

      这里的任何答案,如果没有在谷歌地图上显示最终结果,都违反了上述服务条款,最终是不正确的。

      【讨论】:

      • 这些发现的链接将对所有读者都非常有益。
      • It 10.1 (g) 受许可限制:developers.google.com/maps/terms
      • 那么正确的方法是什么?
      • 使用商业许可证,可以在不显示谷歌地图的情况下获取和显示旅行时间数据。不过,这样的协议必须与谷歌处理。我工作的公司的一个客户正在做这件事。由于我正在开发解决方案,因此我要求他们将他们的 Google 销售代表的确认转发给我。
      • 我猜它改变了:“你可以在谷歌地图上显示距离矩阵 API 结果,或者没有地图。”,来源:developers.google.com/maps/documentation/distance-matrix/…
      【解决方案4】:

      是的,这绝对可以使用 API。您可以构造一个没有地图或方向 div 的 GDirections 对象。您可以从 A 到 B 进行加载请求,然后调用 getDuration 方法获取总行程时间。

      首先你需要创建一个方向对象:

      // no need to pass map or results div since
      // we are only interested in travel time.
      var directions = new GDirections (); 
      

      然后执行加载请求以解析方向(我使用了两个纬度和经度作为起点和终点,但您也可以在此处使用地址):

      var wp = new Array ();
      wp[0] = new GLatLng(32.742149,119.337218);
      wp[1] = new GLatLng(32.735347,119.328485);
      directions.loadFromWaypoints(wp);
      

      然后您需要监听 load 事件,以便在解决方向后调用 getDuration

      GEvent.addListener(directions, "load", function() {
          $('log').innerHTML = directions.getDuration ().seconds + " seconds";
              });
      

      您可以找到whole example hereJavaScript here。您可以查看Google Maps Documentation 以获取有关可以为 GDirections 对象提供的选项的更多信息(例如步行距离等...)。您还应该收听地理编码失败的 error 事件。

      【讨论】:

      • 现在假设我有一个包含 50 个点的 kml 文件。我想知道所有这些点之间的旅行时间,你建议我应该怎么做?
      • 链接失效了!
      • 这似乎产生了理想交通条件下的驾驶时间(忽略当前条件)。无论出发时间如何,结果都是相同的。
      • 整个例子都死了——也许是 api 的变化?
      【解决方案5】:

      来自 google.maps.DirectionsRenderer

      使用距离:

      directionsDisplay.directions.routes[0].legs[0].distance.text
      

      使用时长:

      directionsDisplay.directions.routes[0].legs[0].duration.text
      

      【讨论】:

      • 这似乎产生了理想交通条件下的驾驶时间(忽略当前条件)。无论出发时间如何,结果都是相同的。
      【解决方案6】:

      备案:目前(2015 年)GDirections、GLatLng、GEvent 等已弃用


      您可以使用google.maps.DirectionsService class (Google Maps JavaScript API V3)

      在下面的sn-p中,location和target都是包含经纬度坐标的对象。

      1) google.maps.DirectionsService 类允许 LatLng 和字符串值提供它们的起点和终点属性。
      2) LatLng 是地理坐标中的一个点:纬度和经度。

      var origin = new google.maps.LatLng( location.latitude, location.longitude ); // using google.maps.LatLng class
      var destination = target.latitude + ', ' + target.longitude; // using string
      
      var directionsService = new google.maps.DirectionsService();
      var request = {
          origin: origin, // LatLng|string
          destination: destination, // LatLng|string
          travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
      
      directionsService.route( request, function( response, status ) {
      
          if ( status === 'OK' ) {
              var point = response.routes[ 0 ].legs[ 0 ];
              $( '#travel_data' ).html( 'Estimated travel time: ' + point.duration.text + ' (' + point.distance.text + ')' );
          }
      } );
      

      【讨论】:

        猜你喜欢
        • 2016-01-22
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多