是否只能在Google Directions API中检索步行路程?

时间:2019-11-10 09:13:21

标签: google-maps flutter google-directory-api

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

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

在这里,我使用的是Directions API文档中的示例响应。 here

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,我能够检索整个旅程的到达时间等,但无法检索到公交车站和距公交车站的步行距离和持续时间。这可以做到吗?

打印响应时,我得到这些结果。

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):      

谢谢。

1 个答案:

答案 0 :(得分:2)

在您更新的问题中,您说您请求TRANSIT从芝加哥到洛杉矶的路线。这将包括步行路线,因为对于这种请求,API会将公交车站/火车站视为两个城市的出发地和目的地。

如果您输入准确的地址或坐标(远离出发/到达站),则很可能包括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>