Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps - How to get the distance between two point in metre?

I have these coordinate :

(45.463688, 9.18814)  (46.0438317, 9.75936230000002) 

and I need (trought Google API V3, I think) to get the distance between those 2 points in metre. How can I do it?

like image 568
markzzz Avatar asked Nov 03 '11 15:11

markzzz


People also ask

How do you change distance on Google Maps in meters?

To change the map scale units used on the Google Maps desktop website on your Windows 10 PC or Mac, click the scale bar in the bottom-right corner. Selecting the scale bar will switch to the other measurements, allowing you to quickly view the map scale in miles, kilometers, or relevant smaller units.


2 Answers

If you're looking to use the v3 google maps API, here is a function to use: Note: you must add &libraries=geometry to your script source

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>  <script> var p1 = new google.maps.LatLng(45.463688, 9.18814); var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);  alert(calcDistance(p1, p2));  //calculates distance between two points in km's function calcDistance(p1, p2) {   return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2); }  </script> 
like image 171
Roman Goyenko Avatar answered Sep 30 '22 22:09

Roman Goyenko


I think you could do without any specific API, and calculate distance with plain Javascript:

This site has good info about geographical calculations and Javascript sample for distance calculation.

Ok, quick glance at Google API page and it seems, you could do it by:

  1. Call DirectionsService().route() to get DirectionsResult with routes
  2. For one or each route go through its legs property and calculate sum of distances
like image 45
PiRX Avatar answered Sep 30 '22 21:09

PiRX