Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function toRad(x){
- return (x * Math.PI / 180);
- }
- function calculateDistance(userAddress, providerAddress){
- if(providerAddress && userAddress){
- const lon1 = providerAddress.lat;
- const lat1 = providerAddress.lng;
- const lon2 = userAddress.lat;
- const lat2 = userAddress.lng;
- const R = 6371;
- const x1 = lat2 - lat1;
- const dLat = toRad(x1);
- const x2 = lon2 - lon1;
- const dLon = toRad(x2)
- const angle = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
- Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
- Math.sin(dLon / 2) * Math.sin(dLon / 2);
- const arc = 2 * Math.atan2(Math.sqrt(angle), Math.sqrt(1 - angle));
- const distance = R * arc;
- return Math.ceil(distance || 0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement