Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API - Get points along route between lat/long

I have a web site that I am trying to get completed and I need to have the user click points on a map and then work out the route on the roads between the two points. So the user clicks the first point on 1st street, and then clicks another point on 4th street, and the map will find the best way to get there and plot the route on the map. I am assuming this can be done using directions and parse it up, but I have been searching for an hour now and can't find what I am looking for (maybe bad search terms). I need to be able to plot the map manually (?) so I can calculate the distance, etc... of the route as the user continues to click.

The site that is in beta is http://www.RunMyRoute.com/UserRoutes/Create and you can see I am trying to create running routes. I want the user to have the option for the route to follow the roads versus just a straight line between two points on the map.

Any help on this would be great!

like image 653
SimonSays Avatar asked Apr 07 '10 20:04

SimonSays


People also ask

Can Google Maps API calculate distance between two points?

The API returns information based on the recommended route between start and end points. You can request distance data for different travel modes, request distance data in different units such kilometers or miles, and estimate travel time in traffic.

How do I measure the distance between two latitude longitude points on Google Maps?

The formula for calculating longitude distance is: "Dep = d. long * Cos Mid. Lat" Dep is the same thing as miles.


2 Answers

I had a quick look at the Google Maps API and it looks like you can create a GDirections object to find a route between points and then you can get a PolyLine of the route or things like duration and distance.

like image 196
jondro Avatar answered Sep 29 '22 16:09

jondro


directions.load("from:" + lat1+ ", " + lng1+ " to:" + lat2 + "," + lng2, { getPolyline: true, getSteps: true });

var poly;

GEvent.addListener(directions, "load", function() {
      if (poly) map.removeOverlay(poly);
      poly = directions.getPolyline();
      map.addOverlay(poly);
});
like image 37
Johan Avatar answered Sep 29 '22 15:09

Johan