我是谷歌地图和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"];
}
}
目前,我有这些代码,并查看响应。正文,我可以检索整个旅程的到达时间等,但不能检索到公共汽车站和离开公共汽车站的步行距离和持续时间。这有可能做到吗?
当我打印响应时,我会得到这些结果。正文
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将公共汽车/火车站视为两个城市的起点和终点。
如果你输入了一个精确的地址或坐标(远离出发/到达站(,它很可能包括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>