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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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: