To calculate the distance between two points, it’s just

$data[] = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=Kyiv&destination=Lviv&sensor=false')); 

but how to do if the points are more than 2 here does not work

 $data[] = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=Kyiv&destination=Lviv&waypoints=Vinnica&sensor=false')); 

1 answer 1

did something like this

  $origin = null; $array = array( 'Винница', 'Житомир', 'Львов', 'Ужгород', 'Тернополь', ); foreach ($array as $k => $v) { if ($origin != null) { $data[$k]['origin'] = $origin; $data[$k]['destination'] = $v; } $origin = $v; } $distance = 0; foreach ($data as $ks => $vl) { $data = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=' . $vl['origin'] . '&destination=' . $vl['destination'] . '&sensor=false')); foreach ($data->routes as $k => $v) { foreach ($v->legs as $ke => $va) { echo $va->start_address . '</br>'; echo $va->end_address . '</br>'; echo $va->distance->text . '</br>'; echo '<hr/>'; $distance = $va->distance->text + $distance; } } } echo $distance;