本文介绍了Google路线API返回不同的waypoint_order和路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Google Maps API跟踪路线的系统。
我有起点和终点,在这些点之间有一些航点。
通过追踪Google返回给我的最佳路线并在地图上标记这些点。
我们在div中显示路线数据。
我的函数计算路由,返回数据的部分如下所示:

  directionsService.route(request ,$ .proxy(function(response,status){
if(status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(response);
var orders = response。路线[0] .waypoint_order;
var route = response.routes [0];
var total_distance = 0;
var displayRoute = $('#detail-route'); $ b $对于(i = 0; i
var routeSegment = i + 1,
from_address = route,b

。 leg [i] .start_address.split(','),
to_address = route.legs [i] .end_address.split(',');
total_distance + = Math.floor(route.legs [i] .distance.value / 1000);

displayRoute.append('< b> Trecho'+ routeSegment +':< / b>< br />');
displayRoute.append('< b> Saindo de:< / b>'+ from_address [0] +'< br />');
displayRoute.append('< b> Indo para:< / b>'+ to_address [0] +'< br />');
displayRoute.append('< b> Distine:< / b>'+ route.legs [i] .distance.text);
}

displayRoute.prepend('total:'+ this.format(total_distance)+'km'+'< br />< br />');

函数 format()格式km ..



问题在于,在某些路线上, waypoint_order 显示的顺序不同于在腿部中。例如:

对于给定的路线, route.legs [i] 返回顺序:'waypoint 0, $ 3
$ waypoint_order 属性返回[3,0,2,1,3]

b

这是预期的行为,还是我缺少某些东西?

解决方案

p>

如果optimizeWaypoints设置为true,则此字段将包含输入路点的重新排序的排列。例如,如果输入是:
原产地:洛杉矶

航点:达拉斯,班戈,凤凰城
目的地:纽约
,优化输出的顺序如下:
原产地:洛杉矶
航点:凤凰城,达拉斯,班戈
目的地:纽约
,那么这个字段将是一个包含值[2,0,1]的数组。请注意,航点的编号是从零开始的。
如果任何输入路点的中途停留设置为false,则此字段将为空,因为路线优化不适用于此类查询。



如此处所述:



waypoint_order是优化的订单


I'm developing a system to trace route using Google Maps API.I have the points of origin and destination and between these points there are some waypoints.By tracing the route Google returns me the best route and mark these points on the map.We display the route data in a div.My function that calculates the route, the part that returns the data looks like this:

directionsService.route(request, $.proxy(function(response, status){
  if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      var orders = response.routes[0].waypoint_order; 
      var route = response.routes[0];
      var total_distance = 0;
      var displayRoute = $('#detail-route');


      for (i = 0; i < route.legs.length; i++){

         var routeSegment = i + 1,
         from_address = route.legs[i].start_address.split(','),
         to_address = route.legs[i].end_address.split(',');
         total_distance += Math.floor(route.legs[i].distance.value / 1000);

         displayRoute.append('<b>Trecho ' + routeSegment + ': </b><br/>');
         displayRoute.append('<b>Saindo de: </b>' + from_address[0] + '<br/>');
         displayRoute.append('<b>Indo para: </b>' + to_address[0] + '<br/>');
         displayRoute.append('<b>Distância: </b>' + route.legs[i].distance.text);
      }

  displayRoute.prepend('total:' + this.format(total_distance) + ' km' + '<br/><br/>');

function format() is my function for format km..

The problem is that, on some routes, the waypoint_order shows a different order than the one in legs. For example:

For a given route, the route.legs[i] returns order: 'waypoint 0, waypoint 1, waypoint 3, waypoint 2', but the waypoint_orderattribute returns [3, 0, 2, 1, 3]

Is this the expected behavior, or am i missing something here?

解决方案

Is an expected behaivour since:

If optimizeWaypoints was set to true, this field will contain the re-ordered permutation of the input waypoints. For example, if the input was: Origin: Los Angeles
Waypoints: Dallas, Bangor, Phoenix Destination: New Yorkand the optimized output was ordered as follows: Origin: Los Angeles Waypoints: Phoenix, Dallas, Bangor Destination: New Yorkthen this field will be an Array containing the values [2, 0, 1]. Note that the numbering of waypoints is zero-based.If any of the input waypoints has stopover set to false, this field will be empty, since route optimization is not available for such queries.

As stated here: DirectionsResult

The waypoint_order is the optimized order

这篇关于Google路线API返回不同的waypoint_order和路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:25