Closed korolkovas closed 7 years ago
Hi @korolkovas ,
Please can you prove an example to reproduce the issue ?
Regards, Alexander
Sure, first of all, thanks for sharing the library, it's great and very useful.
$d = \GoogleMaps::load('directions')
->setParam([
'origin' => 'Sao Paulo, SP',
'destination' => 'Sao Paulo, SP',
'mode' => 'driving',
'waypoints' => ['Rua Indico, Sao Bernardo do Campo,SP', 'Santos,SP', 'Rua Aurora, Sao Bernardo do Campo,SP', 'Santos,SP', 'Sorocaba,SP'],
'optimizeWaypoints' => 'true',
'units' => 'metric',
'region' => 'BR',
'departure_time' => 'now',
])
->get();
$teste = json_decode( $d, true );
dd($teste);
It turns out that the response is not getting the optimized waypoints, I can say that because those 2 addresses in bold are in the same city, Santos and Sorocaba are far away cities.
(...) "waypoint_order" => array:5 [ 0 => 0 1 => 1 2 => 2 3 => 3 4 => 4 ] (...)
I got the following response:
(...) "waypoint_order": [ 4, 1, 3, 0, 2 ] (...)
I think the issue is on the placement of waypoint=optimize:true parameter.
https://maps.googleapis.com/maps/api/directions/json?key=<
https://maps.googleapis.com/maps/api/directions/json?key=<
Regards,
Ian
Hi @korolkovas ,
Thank you for detailed response provided.
I have applied changes that allows to pass optional parameter optimize:true
in order to optimize provided route by rearranging waypoints in efficient order.
When making the call please do include optimize:true
as the first argument within the waypoints
array.
....
'waypoints' => ['optimize:true','Rua Indico, Sao Bernardo do Campo,SP',.....'],
....
No need of specifying following parameter : 'optimizeWaypoints' => 'true',
.
Here the amended version of your code
$d = \GoogleMaps::load('directions')
->setParam([
'origin' => 'Sao Paulo, SP',
'destination' => 'Sao Paulo, SP',
'mode' => 'driving',
'waypoints' => ['optimize:true','Rua Indico, Sao Bernardo do Campo,SP', 'Santos,SP', 'Rua Aurora, Sao Bernardo do Campo,SP', 'Santos,SP', 'Sorocaba,SP'],
'units' => 'metric',
'region' => 'BR',
'departure_time' => 'now',
])
->get();
/**
* Full URL generated for given parameters
* https://maps.googleapis.com/maps/api/directions/json?key=_________API_______KEY_________&origin=Sao+Paulo%2C+SP&destination=Sao+Paulo%2C+SP&mode=driving&waypoints=optimize:true|Rua%2BIndico,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CRua%2BAurora,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CSorocaba,SP&units=metric®ion=BR&departure_time=now
*/
$teste = json_decode( $d, true );
dd($teste);
/**
* waypoints order returned
* "waypoint_order" => array:5 [
* 0 => 4
* 1 => 1
* 2 => 3
* 3 => 0
* 4 => 2
* ]
Let me know if you have any questions.
Regards, Alexander
Thank you very much. Now it is working perfectly.
Setting the parameter optimizeWaypoints to true doesn't change the response.
In the example from maps api documentation destinations are set this way: (...)waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA
The curl request in this library adds the optimizeWaypoints=true to the end of the URL which doesn't return the array optimized_waypoints order correctly.