calculate distance

2019-02-21 06:42发布

问题:

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

回答1:

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.



回答2:

Just a thought: If your clients and candidates live in a very limited area, I would use a rectangular map and enter x and y values for all cities in that area in a database.

Due to the pythagorean theorem, you can say:

distance² = (x-value client - x-value candidate)² + (y-value client - y-value candidate)²



回答3:

First, isn't the radius actually the 'as crow flies' distance from the client to the candidates? Anyway...you will need to figure out an algorithm for computing what you need based on the distance between the points.

If you want the system to be stand-alone you will need to have the values for the coordinates (latitude, longitude) of the points. For this, you will probably need and internet connection and the use of a service like google maps to find the coordinates based on the addresses of the points. But once you store then in your db, you will not need an internet connection.

There is a formula for computing the distance between two points based on the coordinates, you will easily find it :)