Fetching all the coordinates along a route generated by Directions Service in Google Maps Apiv3
Asked Answered
K

2

13

My application allows a user to select two points on the map and find a route between them using the Directions Service of the Google maps Apiv3. Then the coordinates along this route has to be saved to the database. I could successfully write all the code to accomplish this. However I am left out with an issue.

I know that there are several other questions in StackOverflow- One, Two on the same, but I think surely either they or me has missed out something here.

Sample Code:

function getCoordinates(result) {
            var currentRouteArray = result.routes[0];  //Returns a complex object containing the results of the current route
            var currentRoute = currentRouteArray.overview_path; //Returns a simplified version of all the coordinates on the path


            obj_newPolyline = new google.maps.Polyline({ map: map }); //a polyline just to verify my code is fetching the coordinates
            var path = obj_newPolyline.getPath();
            for (var x = 0; x < currentRoute.length; x++) {
                var pos = new google.maps.LatLng(currentRoute[x].kb, currentRoute[x].lb)
                latArray[x] = currentRoute[x].kb; //Returns the latitude
                lngArray[x] = currentRoute[x].lb; //Returns the longitude
                path.push(pos);
            }
      }

The above code works perfect except that the kb and lb properties of the overview path which seem to hold the lat and lng coordinates are not always the same. The last time I made the code, it was kb and lb for few days, and later it changed to mb, nb and today jb and kb.

I dont see any other properties in the object that can provide me the latlng's other than the above. Answers to other similar questions dint mention this issue. Am I missing something here? Any reliable solutions please.

Kiddy answered 30/3, 2013 at 10:44 Comment(0)
C
12

do not use kb and lb which are minified names and undocumented. Use only the documented properties .lat() and .lng()

Clamshell answered 30/3, 2013 at 11:56 Comment(0)
H
0

Another option is to decode the "points" string:

http://home.provide.net/~bratliff/routes/

It is a lot faster than using the lat() / lng() methods for every individual point.

Housekeeper answered 30/3, 2013 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.