Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

php - calculate distance

i am designing a recruitment database, and i need it to perform several tasks that all involve integrating a way of calculating distance:

1) calculate the distance the candidate lives from the client?

2) calculate the clients within a radius of the candidates available for work on any given day?

3) calculating the number of candidates with the correct qualifications for the vacancy within a radius of the client.

as you can see i need to calculate the distance in 2 main ways 1) radius 2) as the crow flies, i would prefer exact distance but the first will do. i know that i can integrate Google maps or some other web based mapping but i want the system to be stand alone so it can function without an internet connection. the system will have a HTML5 front end and the Back end is in Mysql and PHP.

thank you biagio

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The distance between 2 points could be calculated with the following formula :

6371*acos(cos(LatitudeA)*cos(LatitudeB)*cos(longitudeB-longitudeA)+sin(LatitudeA)*sin(latitudeB))

Of course it's a "crow flies" approximation in Km.

Wich can be translated to php by :

$longA     = 2.3458*(M_PI/180); // M_PI is a php constant
$latA     = 48.8608*(M_PI/180);
$longB     = 5.0356*(M_PI/180);
$latB     = 47.3225*(M_PI/180);

$subBA       = bcsub ($longB, $longA, 20);
$cosLatA     = cos($latA);
$cosLatB     = cos($latB);
$sinLatA     = sin($latA);
$sinLatB     = sin($latB);

$distance = 6371*acos($cosLatA*$cosLatB*cos($subBA)+$sinLatA*$sinLatB);
echo $distance ;

With that you could compute the distance between two points (people) and of course determine if a point is in a radius of an other.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...