Monday, December 21, 2009

Point of interest along a route (google maps)

Steps to obtain the coordinates of each point returned by Google Maps API on a given route:


1. let's call the direction 'gdir'
gdir = new GDirections(map, document.getElementById("directions"));
2. obtain the shortest route:
var route = gdir.getRoute(0);
3. obtain the number of steps on the route:
var stepn=route.getNumSteps();
4. define your object:
var polyline = gdir.getPolyline();
5. find the first point on the route:
splitvx=polyline.getVertex(0);
6. make a loop through all the steps:
 
for (i=0;i<stepn;i++){}

7. get the coordinates of the first point on the route:

var start=route.getStartGeocode();
8. get the total number of point on the route:

var numPolylineVertices = polyline.getVertexCount();
9. make a loop to extract all the coordinates on the route:


for (n=0;n<numPolylineVertices;n++){

      vertexCoord=polyline.getVertex(n);
      var arr = new Array();
      arr[n]=vertexCoord;}

10. perform a loop against your list of coordinates to get the distances.




Some notes:
Obviously this technique is good on short distances, after that the user's browser will crash.
The  code should be placed in it's suitable place, but a sample you can find here.