Using JavaScript in the browser, how can I determine the distance from my current location to another location for which I have the latitude and longitude?
Using JavaScript in the browser, how can I determine the distance from my current
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your code runs in a browser, you can use the HTML5 geolocation API:
Once you know the current position and the position of your “target”, you can calculate the distance between them in the way documented in this question: Calculate distance between two latitude-longitude points? (Haversine formula).
So the complete script becomes:
Apparently I am 6643 meters from the center of Boston, MA right now (that’s the hard-coded second location).
See these links for more information: