Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is best to calculate distance between two points?

I tried to calculate the distance between two points in Google map. Some of the link i found they are using
distanceTo() and somewhere it is calculated using distanceBetween(). Which is best to calculate distance?

like image 865
MBMJ Avatar asked May 28 '12 09:05

MBMJ


1 Answers

This link might be helpful to you.

it allows to calculate the distance between two latitude longitude points:

This script calculates great-circle distances between the two points (i.e)the shortest distance over the earth’s surface using the ‘Haversine’ formula.

var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; // Distance in km.
like image 170
Hulk Avatar answered Nov 06 '22 19:11

Hulk