0

I have the following arrays:

$latitude = ['9.9663755','10.1883153','9.9689272','10.1109653' ,'10.1883153','10.1883153']
$longitude = ['76.288569','76.4512288','76.2890601','76.3541268','76.4512288','76.4512288']

How do I get the total distance travelled with these points.

Emma
  • 26,487
  • 10
  • 35
  • 65

1 Answers1

0

Except for the array handling this is a duplicate of Measuring the distance between two coordinates in PHP (to calculate the distance). I would suggest something like:

$dist=0;
$alen=count($latitude);

for ($i=1;$i<$alen;$i++) {
   $lat=$latitude[$i];
   $lon=$longotude[$i];
   $frlat=$latitude[$i-1];
   $frlon=$longotude[$i-1]
  // calculate distance according to the link above
  // pass in $frlat, $frlon and $lat, $lon to determine a single
  // segment distance. store in $calcdist)
  $dist+=$caldist;
  $llat=$lat;
  $
}
mlewis54
  • 2,242
  • 6
  • 34
  • 57
  • Rasa Mohmed -- Your proposed edit does not take into account that the loop should only be run 4 times since the first element in the array is the starting point. So you are adding 1-0, 2-1, 3-2 and 4-3 elements to get the distance that's why $i – mlewis54 May 28 '19 at 14:22