alexpechkarev / google-maps

Collection of Google Maps API Web Services for Laravel
https://alexpechkarev.github.io/google-maps/
MIT License
523 stars 115 forks source link

Optimize Waypoints in directions API not working properly #31

Closed korolkovas closed 7 years ago

korolkovas commented 7 years ago

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.

alexpechkarev commented 7 years ago

Hi @korolkovas ,

Please can you prove an example to reproduce the issue ?

Regards, Alexander

korolkovas commented 7 years ago

Sure, first of all, thanks for sharing the library, it's great and very useful.

For example, I'm using your library and with this code below:

$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.

The response of dd() below shows that waypoints are not optimized:

(...) "waypoint_order" => array:5 [ 0 => 0 1 => 1 2 => 2 3 => 3 4 => 4 ] (...)

If I request to google api using their sintax:

http://maps.googleapis.com/maps/api/directions/json?origin=Sao+Paulo,SP&destination=Sao+Paulo,SP&waypoints=optimize:true|Rua+Indico,Sao+Bernardo+do+Campo,SP|Santos,SP|Rua+Aurora,Sao+Bernardo+do+Campo,SP|Santos,SP|Sorocaba,SP&sensor=false

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.

Your code creates a curl request like this one:

https://maps.googleapis.com/maps/api/directions/json?key=<>&origin=Sao+Paulo%2C+SP&destination=Sao+Paulo%2C+SP&mode=driving&waypoints=Rua%2BIndico,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CRua%2BAurora,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CSorocaba,SP&units=metric&region=BR&departure_time=now&optimizeWaypoints=true

Changing the url parameter to the same explained on google documentation made it work.

https://maps.googleapis.com/maps/api/directions/json?key=<>&origin=Sao+Paulo%2C+SP&destination=Sao+Paulo%2C+SP&mode=driving&waypoints=optimize:true%7CRua%2BIndico,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CRua%2BAurora,%2BSao%2BBernardo%2Bdo%2BCampo,SP%7CSantos,SP%7CSorocaba,SP&units=metric&region=BR&departure_time=now

Regards,

Ian

alexpechkarev commented 7 years ago

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&region=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

korolkovas commented 7 years ago

Thank you very much. Now it is working perfectly.