需要在optimizewaypoint之后获得waypoint_order

时间:2015-04-28 16:58:53

标签: javascript php google-maps-api-3

<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>
    <script type="text/javascript">
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var something;

var locations = <?php echo json_encode($googlemaparray);?>;
var uniquelocations= <?php echo json_encode($uniquegoogle);?>;
var barinfo = <?php echo json_encode($barinfoarray);?>;
var marker, i;
var total = uniquelocations.length;
var test;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 17,
    center: new google.maps.LatLng(locations[0],locations[1]),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});


directionsDisplay.setMap(map);

var start = new google.maps.LatLng(uniquelocations[0],uniquelocations[1]);
var end = new google.maps.LatLng(uniquelocations[0],uniquelocations[1]);
var waypts = [];
    for (var i = 2; i < uniquelocations.length; i+=2){
        waypts.push({
        location:new google.maps.LatLng(uniquelocations[i],uniquelocations[i+1])});
    };
var request = {
    origin: start,
    destination: end,
    waypoints:waypts,
    optimizeWaypoints:true,
    travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        var something=response.routes.waypoint_order;
        console.log(something);
    }   
});
//  test = google.maps.geometry.spherical.computeDistanceBetween({
//  from:new google.maps.LatLng(uniquelocations[0],uniquelocations[1]),
//  to:new google.maps.LatLng(uniquelocations[2],uniquelocations[3])
//  });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

如何从waypoint_order获取对数组的访问权限,因为我需要知道我的路点是如何排序的。当我为现有代码安装console.log(某些东西)时,它会说&#34; undefined&#34;在控制台中。我正在使用WAMP堆栈来测试此代码。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

航点顺序为response.routes[0].waypoint_order(如果返回多个航线,则0为第一条航线)。