Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7983957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:07:06+00:00 2026-06-04T11:07:06+00:00

I have strange problem with the Google Maps DirectionsService. It returns me different routes

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:07:08+00:00Added an answer on June 4, 2026 at 11:07 am

    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 come 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 completed
             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);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem with Google Maps showing differently in IE and the
I have a strange problem with .htaccess and google. I have multiple-different root domain
i have strange problem doing reporting: i have numerous clients with different issued invoices.
I have strange problem with receiving data from socket. On client im using air
I have a strange problem with some documents on my webpage. My data is
I have ran into a very strange issue with Google Maps in Chrome 5.0.375.99:
I have just encountered very strange problem - my GWT app hosted on Google
I found strange problem. I used this basic example of google maps. This maps
I have a strange problem with css file in google app engine. If i
I have spent hours on a strange problem with the interpolate function of google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.