php - Distance calculation based on multiple locations (Lat,Lon) including starting/ finishing point -


i calculate total distance of driving beetween multiple locations (loop), including distance (starting point (garage) - first location sarting point) , (last location finishig point - finishing point (garage)). example: (garage + d1) + (d1 + d2) + (d2 + e1) + (e1 + e2) + e2 + garage)

i'm having problem correct looping. here's simplified code:

<? $driver = 5;       $result2 = mysql_query("select * test id='$driver' limit 1") or die(mysql_error());      while($row2 = mysql_fetch_array( $result2 )) {          $lon=$row2['lon'];          $lat=$row2['lat'];     echo "$lon, $lat";      }     $result = mysql_query("select * test1 driver='$driver'") or die(mysql_error());       while($row = mysql_fetch_array( $result )) {           $lon1=$row['lon1'];          $lat1=$row['lat1'];          $lon2=$row['lon2'];          $lat2=$row['lat2'];          //////////  distance between driver address , starting address             $distancecalc = (3958*3.1415926*sqrt(($lat-$lat1)*($lat-$lat1) + cos($lat/57.29578)*cos($lat1/57.29578)*($lon-$lon1)*($lon-$lon1))/180);         //////////  distance between statring address , finishing address  - multiple adsresses         $distancecalc1 = $distancecalc1 + (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);         //////////  distance between finishing address , driver address         $distancecalc2 = (3958*3.1415926*sqrt(($lat2-$lat)*($lat2-$lat) + cos($lat2/57.29578)*cos($lat/57.29578)*($lon2-$lon)*($lon2-$lon))/180);          $distancetotal = $distancecalc + $distancecalc1 +$distancecalc2;          echo "$distancecalc<br>         $distancecalc1<br>         $distancecalc2<br>";     }     echo "$distancetotal";   ?> 

i tried things (mostly if... ) , more database requests, i'm still having problem of avoiding multiple calculations, , believe there way code make easier , clearer.

i appreciate on one.

thank much.

here's function need:

function distance ($lat1, $lon1, $lat2, $lon2) {     return (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180); } 

the function body same formula used in-line, don't understand why needed this.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -