Advertisement
rAthus

[PHP+MySQL] trier par distance (latitude longitude)

Mar 2nd, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. /***********/
  2. /* Données */
  3. /***********/
  4.  
  5. $lat = 45.763140; // latitude
  6. $lng = 4.828063; // longitude
  7. $rayon_en_m = 2000; // rayon maximum de recherche en mètres, false si on ne veut pas de limite
  8.  
  9. /***********/
  10. /* Calculs */
  11. /***********/
  12.  
  13. $latlngMarge = $rayon_en_m*0.009;
  14. $query = 'SELECT *, 3956*2*ASIN(SQRT(POWER(SIN(('.$lat.'-ABS(lat))*PI()/180/2),2)+COS('.$lat.'*PI()/180)*COS(ABS(lat)*PI()/180)*POWER(SIN(('.$lng.'-lng)*PI()/180/2),2)))*1609.34 AS distance_m FROM table WHERE 1'.(($rayon_en_m)?' HAVING distance_m<='.$rayon_en_m:'').' ORDER BY distance_m ASC';
  15.  
  16. // la variable distance_m vaut la distance à vol d'oiseau en mètres
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement