I want to use this formula with php. I have a database with some values of latitute and longitude saved.
I want to find, with a certain value of latitude and longitude in input, all the distances (in km) from this point with each point in the database. To do this, I used the formula on googlemaps api:
( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )
Of course using that in php I replaced radians with deg2rad
.The values 37,-122 are my values of input and lat,lng are my values in the database.
Below there is my code. The problem is that there is something wrong but I don't understand what. The value of distance is of course wrong.
//values of latitude and longitute in input (Rome - eur, IT)
$center_lat = "41.8350";
$center_lng = "12.470";
//connection to database. it works
(..)
//to take each value in the database:
$query = "SELECT * FROM Dati";
$result = mysql_query($query);
while ($row = @mysql_fetch_assoc($result)){
$lat=$row['Lat']);
$lng=$row['Lng']);
$distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) * (cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) * (sin(deg2rad($lat))))) );
}
For values for example:
$lat= 41.9133741000
$lng= 12.5203944000
I have the output of distance="4826.9341106926"
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…