Created
January 6, 2016 18:44
-
-
Save guiassemany/e0ba8a1da6d618259801 to your computer and use it in GitHub Desktop.
Calculate Distance Between two points on a map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Calculate Distance Between two points on a map</title> | |
| </head> | |
| <body> | |
| <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script> | |
| <script type="text/javascript"> | |
| var p1 = new google.maps.LatLng(-12.928646, -38.509659); | |
| var p2 = new google.maps.LatLng(-12.929426, -38.517798); | |
| console.log(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> | |
| </body> | |
| </html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment