多次虚假中途停留会导致路线搜索失败

时间:2014-02-14 15:15:03

标签: google-maps-api-3

我在一组Lat / Lng坐标上运行了两次查询,并且在有多次中途停留时收到失败的结果。

  • 当中途停留:在单个航路点上设置为false时,搜索会正确返回
  • 当中途停留:在多个航点上设置为false时,搜索以ZERO_RESULTS返回。
  • 当中途停留:在任意数量的航路点上设置为true时,航路点似乎从方向距离被解除。

    // Query 1 uses the format: Pickup - Taxi rank coods - Destination:
    query = 'from: 50.899083,0.040018 to: 50.875629,0.017858 to: 50.972639,0.016789';
    
    
    // Query 2 uses the format: Pickup - Waypoint 1 - Waypoint 2 - Destination
    query = 'from: 50.899083,0.040018 to: 50.82988,-0.14095 to: 50.87077,0.01328 to: 50.972639,0.016789';
    
    
    var start = query.split('from: '),
        end = start[1].split(' to: '),
        jLength = end.length,
        routeDistance = [],
        jWaypoints = [],
        request,
        i;
    
    start = end[0];
    // At this point, start = 50.899083,0.040018
    
    if( (jLength - 2) > 0 ) {
        for (i = jLength - 1; i >= 0; i--) {
            if( i != ( jLength - 1 ) && i != 0 ) {
                jWaypoints.push( {
                    location: end[i],
                    stopover: false
                });
            }
        }
    }
    end = end[jLength - 1];
    
    // end = 50.972639,0.016789
    // jWaypoints = [Object { location="50.87077,0.01328", stopover=false}, Object { location="50.82988,-0.14095", stopover=false}];
    
    
    if( jWaypoints.length >= 1 ) {
        request = {
            origin:start,
            destination:end,
            waypoints: jWaypoints,
            optimizeWaypoints: false,
            travelMode: google.maps.TravelMode.DRIVING
        };
    } else {
        request = {
            origin:start,
            destination:end,
            travelMode: google.maps.TravelMode.DRIVING
        };
    }
    
    
    directions.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            routeDistance.push(response.routes[0].legs[0].distance.value, response.routes[0].legs[0].duration.value);
            passDistance(callback, stopnames, latlons, queries, queryindex, results, routeDistance);
        } else {
            console.log( status );
        }
    });
    
    // Query 1 comes out as: [20560, 1472] -> Correct
    // Query 2 comes out as: ZERO_RESULTS
    

/ - 编辑 - /

发现,当中途停留设置为真时,仅拾取最终航路点。仍然没有更明智的。

2 个答案:

答案 0 :(得分:0)

如果你想使用航点的坐标,它必须是一个google.maps.LatLng对象,而不是一个包含两个以逗号分隔的数字的字符串。

答案 1 :(得分:0)

问题在于警告他们之前最后的距离。通过循环遍历所有支路并对结果求和,返回正确的距离。由此:

 routeDistance.push(response.routes[0].legs[0])

为:

jDistance = 0;

for(i = 0, i >= response.routes[0].legs.length, i++) {
    jDistance += response.routes[0].legs[i];
}

routeDistance.push(jDistance);