是否可以仅检索 google 方向 API 的步行旅程?

Is it possible to only retrieve the walking journey in google directions API?

我是 google 地图和 Flutter 的新手,我想知道是否可以在用户输入目的地时检索步行距离和持续时间。

例如,用户输入他们的工作地点。从这个用户家到公交车站的步行距离将是第一个要检索的距离和持续时间,然后当用户下车时,从公交车站步行到工作地点是另一个要检索的距离和持续时间。

此处我使用了来自说明 API 文档的示例响应。 https://developers.google.com/maps/documentation/directions/intro#DirectionsResponseElements

class GoogleMapsServices{
  Future<String> getRouteCoordinates()async{
    String url = "https://maps.googleapis.com/maps/api/directions/json? 
origin=Chicago,IL&destination=Los+Angeles,CA 
&waypoints=Joplin,MO|Oklahoma+City,OK &key=$apiKey";

    http.Response response = await http.get(url);
    print (response.body);

    Map values = jsonDecode(response.body);
    return values["routes"][0]["overview_polyline"]["points"];

  }

}

目前我有这些代码并查看 response.body,我能够检索到整个旅程的到达时间等,但不能检索到公交车站和远离公交车站的步行距离和持续时间.这可以做到吗?

我在打印 response.body

时得到了这些结果
I/flutter (23129): {
I/flutter (23129):    "geocoded_waypoints" : [
I/flutter (23129):       {
I/flutter (23129):          "geocoder_status" : "OK",
I/flutter (23129):          "place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
I/flutter (23129):          "types" : [ "locality", "political" ]
I/flutter (23129):       },
I/flutter (23129):       {
I/flutter (23129):          "geocoder_status" : "OK",
I/flutter (23129):          "place_id" : "ChIJ69Pk6jdlyIcRDqM1KDY3Fpg",
I/flutter (23129):          "types" : [ "locality", "political" ]
I/flutter (23129):       },
I/flutter (23129):       {
I/flutter (23129):          "geocoder_status" : "OK",
I/flutter (23129):          "place_id" : "ChIJgdL4flSKrYcRnTpP0XQSojM",
I/flutter (23129):          "types" : [ "locality", "political" ]
I/flutter (23129):       },
I/flutter (23129):       {
I/flutter (23129):          "geocoder_status" : "OK",
I/flutter (23129):          "place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
I/flutter (23129):          "types" : [ "locality", "political" ]
I/flutter (23129):       }
I/flutter (23129):    ],
I/flutter (23129):    "routes" : [
I/flutter (23129):       {
I/flutter (23129):          "bounds" : {
I/flutter (23129):             "northeast" : {
I/flutter (23129):                "lat" : 41.8781139,
I/flutter (23129):                "lng" : -87.6297872
I/flutter (23129):             },
I/flutter (23129):             "southwest" : {
I/flutter (23129):                "lat" : 34.0523523,
I/flutter (23129):                "lng" : -118.2435731
I/flutter (23129):             }
I/flutter (23129):          },
I/flutter (23129):          "copyrights" : "Map data ©2019 Google, INEGI",
I/flutter (23129):          "legs" : [
I/flutter (23129):             {
I/flutter (23129):      

谢谢。

在您更新的问题中,您说您请求 TRANSIT 从芝加哥到洛杉矶的路线。这将 包括步行路线,因为对于这种请求,API 将 bus/train 车站视为两个城市的起点和终点。

如果您输入精确的地址或坐标(远离 departure/arrival 个站点),它很可能包括 WALKING 个步骤:

这里是一个使用JS的例子API,但是结果应该和web服务是一样的。在地图下方查看我打印旅行的第一步和最后一步的位置。

function initialize() {

  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer();
  var center = new google.maps.LatLng(0, 0);
  var myOptions = {
    zoom: 7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: center
  }

  var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
  directionsDisplay.setMap(map);

  var start = "41.925704, -87.634690";
  var end = "34.021617, -118.355122";
  var method = 'TRANSIT';
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.DirectionsTravelMode[method]
  };

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      console.log(response);
      directionsDisplay.setDirections(response);

      // Show first step
      $('.steps').append('<li>1. Distance: ' + response.routes[0].legs[0].steps[0].distance.text + '<br>Instructions: ' + response.routes[0].legs[0].steps[0].instructions + '<br>Travel mode: ' + response.routes[0].legs[0].steps[0].travel_mode + '</li>');
      
      var lastStep = response.routes[0].legs[0].steps.length -1;
      // Show last step
      $('.steps').append('<li>' + lastStep + '. Distance: ' + response.routes[0].legs[0].steps[lastStep].distance.text + '<br>Instructions: ' + response.routes[0].legs[0].steps[lastStep].instructions + '<br>Travel mode: ' + response.routes[0].legs[0].steps[lastStep].travel_mode + '</li>');
    }
  });

}
#map-canvas {
  height: 200px;
}

li {
  margin-bottom: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize">
</script>

<ul class="steps"></ul>