Get only one data

Good morning,

How can I just retrieve the distance or arrived data in the php code of the api?

Thanks for your reply

// Set the locations
$locations = array();

$locations[] = array(
	'name' => '1',
	'lat' => 48.855801150341186,
	'lng' => 2.3450850044510143,
);

$locations[] = array(
	'name' => '2',
	'lat' => 48.86122225002856,
	'lng' => 2.3378323117682145,
);

$locations[] = array(
	'name' => '3',
	'lat' => 48.87335771205951,
	'lng' => 2.3306536522423977,
);

$locations[] = array(
	'name' => '4',
	'lat' => 48.874834646124924,
	'lng' => 2.2951331305741998
);

$locations[] = array(
	'name' => '5',
	'lat' => 48.86230926806364, 
	'lng' => 2.3934157883143388,
//	'restrictions' => array(
//		'ready' => 15,
//		'due' => 60
//	)
);
$locations[] = array(
	'name' => '6',
	'lat' => 48.855801150341186,
	'lng' => 2.3450850044510143,
);

// Init API connector class
$r = new API_Connector();

// Get tour
if ($r->tour($locations)) {
	
	// Show result
	print_r($r->result);
}
else {
	
	// Error message
	echo 'ERROR: ' . $r->error;
	
}

?>

Not sure if we understand your question correctly, but the $r->result has an array route with the sorted locations by the algorithm. Each location has its estimated arrival in minutes and distance in km. If you get these from the last item in the array, that’s the total duration and distance for the route.

I will try to express myself better.
When I try to retrieve just the first arrival and distance of the first location after departure from result I get this error.

Warning : Undefined variable $result in ******* on line 149

Warning : Trying to access array offset on value of type null in ******* on line 149

Fatal error : Uncaught TypeError: array_values(): Argument #1 ($array) must be of type array, null given in ***:149 Stack trace: #0 ***(149): array_values(NULL) #1 {main} thrown in ******** on line 149

Our example code does not have a line 149, so it seems to be an error in your code and you’ll want to debug that locally.

PS- If you wrote your code after our first answer, you may need to replace $result with $r->result, our bad. We edited our answer for this too.

I solve my problem, thanks for your help

Have a nice day

1 Like