after hous of search i used this package https://github.com/alexpechkarev/google-maps
with those parametres below :
$origin = ['lat' => 35.762288, 'lng' => -5.866525, 'ville' => 'tanger'];
$destination = ['lat' => 33.565567, 'lng' => -7.552402, 'ville' => 'casablanca'];
$points = [
['lat' => 33.951825, 'lng' => -6.839634, 'ville' => 'rabat'],
['lat' => 35.475028, 'lng' => -6.015167, 'ville' => 'assilah'],
['lat' => 35.178713, 'lng' => -6.132972, 'ville' => 'laarach'],
['lat' => 34.868921, 'lng' => -6.247939, 'ville' => 'moulay Bouslham'],
['lat' => 33.834129, 'lng' => -6.054144, 'ville' => 'khmissat'],
['lat' => 35.574788, 'lng' => -5.360490, 'ville' => 'Tetouan'],
];
$wayPointsString = collect($points)->transform(function ($point) {
return $point['lat'] . ',' . $point['lng'];
})->implode('|');
$data = \GoogleMaps::load('directions')
->setEndpoint('json')
->setParam([
'origin' => collect($origin)->take(2)->implode(','),//tanger
'destination' => collect($destination)->take(2)->implode(','), // casablanca
'waypoints' => ['optimize:true|' . $wayPointsString
],
'mode' => 'driving',
'alternatives' => true,
'departure_time' => 'now',
])
->get();
$data = json_decode($data, true);
$points_ = [];
$points_[] = $origin;
foreach ($data['routes'][0]['waypoint_order'] as $key => $value) {
$points_[] = $points[$value];
}
$points_[] = $destination;
dd($points_);
as a response :
array:8 [
0 => array:3 [
"lat" => 35.762288
"lng" => -5.866525
"ville" => "tanger"
]
1 => array:3 [
"lat" => 35.574788
"lng" => -5.36049
"ville" => "Tetouan"
]
2 => array:3 [
"lat" => 35.475028
"lng" => -6.015167
"ville" => "assilah"
]
3 => array:3 [
"lat" => 35.178713
"lng" => -6.132972
"ville" => "laarach"
]
4 => array:3 [
"lat" => 34.868921
"lng" => -6.247939
"ville" => "moulay Bouslham"
]
5 => array:3 [
"lat" => 33.834129
"lng" => -6.054144
"ville" => "khmissat"
]
6 => array:3 [
"lat" => 33.951825
"lng" => -6.839634
"ville" => "rabat"
]
7 => array:3 [
"lat" => 33.565567
"lng" => -7.552402
"ville" => "casablanca"
]
]