google maps api v3 - 在响应对象中未返回航点

时间:2014-06-24 03:07:08

标签: google-maps-api-3

我有以下代码,我用一个路点发送requestObj。但结果显示我有一条有两条腿和零航路点的路径。我想保留航点。我做错了什么?

var requestObj = {
origin: new google.maps.LatLng(sn['lat'],sn['lng']),
destination: new google.maps.LatLng(en['lat'],en['lng']),
travelMode: google.maps.DirectionsTravelMode.DRIVING,
waypoints: new google.maps.LatLng(45.00334,-73.00228),
optimizeWaypoints: true
};

directionsService.route(requestObj, function(response, status) {

if (status == google.maps.DirectionsStatus.OK) {
   //process data. no way points found in :
   //directionsDisplay.directions.routes[0].legs[0].via_waypoints.length
}});

1 个答案:

答案 0 :(得分:0)

请参阅the documentation

中的示例
  

航点由以下字段组成:

     

location(必需)指定航点的地址。   stopover(可选)指示此航路点是否是路线上的实际停靠点(true)或   而只是优先选择路由指示的位置(错误)。中途停留是   默认为true。

来自API reference

  

航点|阵列。 |中间航点的数组。方向会   通过该阵列中的每个航路点从原点到目的地计算。   允许的最大航点为8,加上原点和目的地。 Maps API for   商业客户可以获得23个航点,以及起点和目的地。航点>运输路线不支持。可选的。

您没有传递有效的航点数组。见下文:

var requestObj = {
origin: new google.maps.LatLng(sn['lat'],sn['lng']),
destination: new google.maps.LatLng(en['lat'],en['lng']),
travelMode: google.maps.DirectionsTravelMode.DRIVING,
waypoints: [{location:new google.maps.LatLng(45.00334,-73.00228)}],
optimizeWaypoints: true
};

directionsService.route(requestObj, function(response, status) {

if (status == google.maps.DirectionsStatus.OK) {
   //process data. no way points found in :
   //directionsDisplay.directions.routes[0].legs[0].via_waypoints.length
}});

working fiddle