alexpechkarev / google-maps

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

Directions with LatLng not working #53

Closed padre closed 6 years ago

padre commented 6 years ago

Hi,

if I try:

\GoogleMaps::load('directions') ->setParam([ 'origin' => "Barcelona, ES", 'destination' => "Madrid, ES", ]) ->get();

works properly.

When I try:

\GoogleMaps::load('directions') ->setParam([ 'origin' => ['lat' => 41.3850639, 'lng' => 2.1734035], 'destination' => ['lat' => 40.4167754, 'lng' => -3.7037902], ]) ->get();

the response is:

{"geocoded_waypoints":[{"geocoder_status":"ZERO_RESULTS"},{"geocoder_status":"ZERO_RESULTS"}],"routes":[],"status":"NOT_FOUND"}

Someone has been able to use directions API with lat/lng parameters?

Thanks.

alexpechkarev commented 6 years ago

Hi @padre,

As per Directions API If you pass coordinates, they are used unchanged to calculate directions. Ensure that no space exists between the latitude and longitude values.

origin=41.43206,-81.38992

Alternatives are to use an address as origin and destination or use place_id, see examples below.

` $d['a'] = \GoogleMaps::load('directions')

                ->setParamByKey('origin', 'Toronto')
                ->setParamByKey('destination', 'Montreal')     
                ->get();

         $d['b'] =   \GoogleMaps::load('directions')
                ->setParam([
                    'origin'          => 'place_id:ChIJ685WIFYViEgRHlHvBbiD5nE', 
                    'destination'     => 'place_id:ChIJA01I-8YVhkgRGJb0fW4UX7Y', 
                    'mode'            => 'driving',
                    'waypoints'       => 'Charlestown,MA|Lexington,MA',// can also be given as an array ['Charlestown,MA','Lexington,MA']
                    'alternatives'    => true,
                    'avoid'           => 'ferries',
                    'units'           => 'metric',
                    'region'          => 'GB',
                    'departure_time'  => 'now',
                ])

               ->getResponseByKey('routes.legs.steps.html_instructions');

`

Regards, Alexander

padre commented 6 years ago

You're right. It's working properly.

Thank you very much.

alexpechkarev commented 6 years ago

Glad to hear that all resolved!