2

I am developing an application via the Google Maps API (Version 3).

I have 2 google.maps.LatLng objects named loc1 and loc2.

How do I calculate the distance between them in say meters?

I have tried using various tricks from this question to convert latitude/longitude measures into meters. However, I question the reliability of my implementation.

I have also tried implementing a distance matrix, but I think that a distance matrix is a lot of work just to calculate the distance between 2 points.

Thank you!

Community
  • 1
  • 1
dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • Does this help? http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3 – vascop Mar 18 '12 at 19:06

4 Answers4

9

Sounds simple, I recommend the geometry library,

https://developers.google.com/maps/documentation/javascript/reference#spherical

computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)

From my experience the result is in meters. Remember to add the optional geometry library in your tag

<script type="text/javascript" 
src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">
</script>
Heitor Chang
  • 6,038
  • 2
  • 45
  • 65
1

Calculating distances between two points basically boils down to pythagoras (sum of the squares etc). However, this doesn't really work with Lat & Lon (which are coordinates on a sphere). What I do is convert the Lat & Lon values to the local map grid which is flat. Then I can use pythagoras to calculate the distance between those points. I've already posted on this in Stack Overflow here. This will have all the code you would need.

Community
  • 1
  • 1
Steve Mc
  • 3,433
  • 26
  • 35
  • Dear me. Conversion to (say) OSGB is hugely more work than using the haversine calculation to get distance as the geometry library does. And using the geometry library makes everything simple -- the haversine calculation can be tricky. – Andrew Leach Mar 18 '12 at 20:12
  • Basically my argument is that once you have done your conversion everything is simple triangle geometry. Typically in mapping applications there is a requirement to convert coordinates to the local map grid anyway. And YES my solution does not involve the geometry library. – Steve Mc Mar 21 '12 at 19:33
1

Your question is a bit ambiguous.

I mean - when you say the distance, do mean

  • distance in a straight line
  • or driving distance,
  • or bicycle distance,
  • or walking distance. The answer to this question will determine your implementation.

If it is a straight line distance you need, then it is best to use the haversine implementation. Otherwise, you will need to request directions from the google directions service, specifying the mode of "transport".

Simon
  • 2,208
  • 4
  • 32
  • 47
  • Google's distance matrix API should help solve this problem. It also considers the driving/ walking distance. Check this link https://developers.google.com/maps/documentation/distance-matrix/ – Sriram Dec 03 '16 at 01:54
-1

When you've finished you can test the distance between two points on a straight line using the provided map. It will give you a visual as well as the distance.