A
A
Abc Edc2015-03-14 11:56:36
PHP
Abc Edc, 2015-03-14 11:56:36

How to calculate the distance in kilometers by the coordinates of longitude and latitude?

A request comes from the client with its gps (android, ios) data and I make a request to mysql to select the object closest to it that I need (just the longitude latitude object is stored there)

$arrayToSort[$i]['pos'] =(abs($value[0] - $userPos[0]) + abs($value[1] - $userPos[1]));

That is, from the longitude of the object, I subtract the longitude of the user modulo and the same thing with the latitude
, and so iterations for each of the objects,
then
usort($ArrReadyToSort, "cmp");
sort and take the zero element, in this case, according to the sort result, this is the closest one, and so
in the end, the closest one is an associative array with a coefficient
$arrayToSort[$i]['pos'] =(abs($value[0] - $userPos[0]) + abs($value[1] - $userPos[1]));

this action
and two other elements are the points of the user and the points of this nearest object, you either need to somehow convert the coefficient into kilometers (ideally) or curl into the google api and get the distance by giving points or return to the client and make a zarpos from it in api ( which is finally not fast)
so the question is how, having the closest object to the user, the points of the user and the object and the coefficient above, find the distance in km / m / at least in something

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-03-14
@gleber1

Haversine formula:

const double R=6371;  // Earth's radius
double sin1=sin((lat1-lat2)/2);
double sin2=sin((lon1-lon2)/2);
return 2*R*asin(sqrt(sin1*sin1+sin2*sin2*cos(lat1)*cos(lat2)));

where lat1,lat2 are the latitudes and lon1,lon2 are the longitudes of the points expressed in radians.
Is it worth linking the library for this?

J
Julia Gusak, 2021-05-04
@Delphin2016

Good day, dear ones! )
I have a similar problem, but I can't figure out how to apply this formula in MySQL.
I need to calculate the total distance traveled from a list of coordinates, preferably in km, or meters for a given period of time.
SELECT SUM )
FROM `data`
WHERE `ID_Dev` = 1 and
`DateTime` BETWEEN '2021-05-04 08:00:00' AND '2021-05-04 20:00:00'
Can someone help me build a MySQL query to this formula processed the coordinates and gave the total total distance?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question