最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does the Google Maps DirectionsService return different routes? - Stack Overflow

programmeradmin3浏览0评论

I have strange problem with the Google Maps DirectionsService. It returns me different routes if input data is the same. Here is a sample of my code

var path = [];
path.push(new google.maps.LatLng(51.10600101811778, 17.025117874145508));
path.push(new google.maps.LatLng(51.1047951799623,17.02278971672058));
path.push(new google.maps.LatLng(51.10456276619924, 17.02208161354065));
path.push(new google.maps.LatLng(51.10131895649719, 17.029248476028442));
path.push(new google.maps.LatLng(51.100331957810134, 17.033969163894653));
path.push(new google.maps.LatLng(51.10001867395775, 17.032413482666016));

for (var i = 0; i <path.length-1; i++) {
    var request = {
        origin: path[i],
        destination: path[i+1],
        travelMode: google.maps.DirectionsTravelMode.WALKING
    }

    directionsService.route(request, function(results, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            selected_path = selected_path.concat(results.routes[0].overview_path);
            poly.setPath(selected_path);
            poly.setMap(map);
        }
    })
}

First time after the call, a function draws a strange polyline that always connects the start point with end point:

The second time it is called, the function works well and the route is drawn properly:

It's only example of input static data. Normally I work with dynamic data on matrix and dynamic markers, but always the same goes on. Firstly, start point is connected with end point + strange connection between other points. Second call function works well. Does somebody of you have some clue how to solve this problem?

I have strange problem with the Google Maps DirectionsService. It returns me different routes if input data is the same. Here is a sample of my code

var path = [];
path.push(new google.maps.LatLng(51.10600101811778, 17.025117874145508));
path.push(new google.maps.LatLng(51.1047951799623,17.02278971672058));
path.push(new google.maps.LatLng(51.10456276619924, 17.02208161354065));
path.push(new google.maps.LatLng(51.10131895649719, 17.029248476028442));
path.push(new google.maps.LatLng(51.100331957810134, 17.033969163894653));
path.push(new google.maps.LatLng(51.10001867395775, 17.032413482666016));

for (var i = 0; i <path.length-1; i++) {
    var request = {
        origin: path[i],
        destination: path[i+1],
        travelMode: google.maps.DirectionsTravelMode.WALKING
    }

    directionsService.route(request, function(results, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            selected_path = selected_path.concat(results.routes[0].overview_path);
            poly.setPath(selected_path);
            poly.setMap(map);
        }
    })
}

First time after the call, a function draws a strange polyline that always connects the start point with end point:

The second time it is called, the function works well and the route is drawn properly:

It's only example of input static data. Normally I work with dynamic data on matrix and dynamic markers, but always the same goes on. Firstly, start point is connected with end point + strange connection between other points. Second call function works well. Does somebody of you have some clue how to solve this problem?

Share Improve this question edited May 20, 2012 at 20:37 Sean Mickey 7,7262 gold badges34 silver badges58 bronze badges asked May 20, 2012 at 14:38 Martus0Martus0 6771 gold badge11 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

It's only a guess: I believe what's happening is the path is getting concatenated out of order, because the directions requests are asynchronous. (Data does not necessarily e back in order). What I did below is place each leg of the directions in order in an array, then flattening the array into a continuous list, and display only after the right number of requests came back successfully.

I couldn't reproduce your zigzag so I don't exactly know if this answer will really solve the problem.

// count number of directions requests that returned successfully
var count = 0;

for (var i = 0; i <path.length-1; i++) {
  var request = {
    origin: path[i],
    destination: path[i+1],
    travelMode: google.maps.DirectionsTravelMode.WALKING
  };

  // introduce variable scope to preserve value of i
  (function(i) {
     directionsService.route(request, function(results, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         selected_path[i] = results.routes[0].overview_path;
         count++;

         // draw path only if all segments are pleted
         if(count == path.length-1) {
           //flatten selected_path into 1-d array
           selected_path = Array.prototype.concat.apply([], selected_path);
           poly.setPath(selected_path);
           poly.setMap(map);
         }
       }
     });
   })(i);
 }

I had the same problem. You can call the function by a timer and set it to 250 millisecond to give you the best result. When you call service by "for" loop this problem will happen.

Here is the method:

//Counter for timer.
var counter2 = 0;

//Set a timer to pass latitude and longitude values to "draw_line()" function.
var timer = setInterval(function(){draw_way(new google.maps.LatLng(arrayLatitude[counter2], arrayLongitude[counter2]));

counter2 = counter2 + 1;

//Check if calue of timer is > "arrayLatitude.length" because if it bees bigger it can't draw the way.


if(counter2 >= arrayLatitude.length)
{
    clearInterval(timer);


}
}, 250);
发布评论

评论列表(0)

  1. 暂无评论