Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps - plot lines and distance (as the crow flies) between multiple latitude longitude pairs

I'm looking to plot a route "as the crow flies" on a map via multiple latitude longitude pairs. I also need to calculate the total distance (and preferrably distance between points).

I have found this example http://www.daftlogic.com/projects-advanced-google-maps-distance-calculator.htm but it is far more complicated than I require - I don't need any kind of search functionality, as I already have my latitude longitude data. However, the way it displays the route is what I'm looking to achieve

Are there any simpler examples out there that I can build upon or any general guidance on achieving this?

like image 376
BrynJ Avatar asked Feb 24 '23 20:02

BrynJ


2 Answers

Plotting a route "as the crow flies" is easy, just set polyline's geodesic option to true:

var geodesic = new google.maps.Polyline({geodesic: true});

You can use this example: http://code.google.com/apis/maps/documentation/javascript/examples/geometry-headings.html

To calculate the distance, you can utilize geometry library of the Google Maps API V3. http://code.google.com/apis/maps/documentation/javascript/geometry.html

The calculation is simple:

var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngOfPointA, latLngOfPointB);

To compute the length of a path you can use computeLength function:

var path = geodesic.getPath();
var length = google.maps.geometry.spherical.computeLength(path)
like image 66
Tomik Avatar answered Apr 05 '23 22:04

Tomik


Be sure to include the geometry library in your call to Google Maps API.

Bootstrap from Documentation

<script type="text/javascript"       src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=true_or_false">  </script>
like image 35
user600830 Avatar answered Apr 05 '23 23:04

user600830