src/Services/Google/DistanceMatrixAPI.php
public function calculateDistanceMatrix(array $origins, array $destinations) : DistanceMatrixResponse
    {
        $formattedOrigins      = $this->formatEntities($origins);
        $formattedDestinations = $this->formatEntities($destinations);

        $query = http_build_query([
            'origins'      => $formattedOrigins,
            'destinations' => $formattedDestinations,
            'key'          => $this->apiKey,
        ]);

        $url  = $this->generateBaseUrl() . '?' . $query;
        $data = $this->processRequest($url);

        if (!$this->checkResponseStatus($data)) {
            $data = $this->processRequest($url);
            if (!$this->checkResponseStatus($data)) {
                throw new ServiceProviderException('GoogleDistanceMatrix returned an unknown_error after retry');
            }
        }

        if (!array_key_exists('rows', $data)) {
            throw new ServiceProviderException('Missing key "rows" on data: ' . print_r($data, true));
        }

        return new DistanceMatrixResponse(
            $data['rows'],
            explode('|', $formattedOrigins),
            explode('|', $formattedDestinations),
            $data['origin_addresses'],
            $data['destination_addresses']
        );
    }
public function calculateDistanceMatrix(array $origins, array $destinations) : DistanceMatrixResponse
    {
        $formattedOrigins      = $this->formatEntities($origins);
        $formattedDestinations = $this->formatEntities($destinations);

        $query = http_build_query([
            'origins'      => $formattedOrigins,
            'destinations' => $formattedDestinations,
            'key'          => $this->apiKey,
        ]);

        $url  = $this->generateBaseUrl() . '?' . $query;
        $data = $this->processRequest($url);

        if (!$this->checkResponseStatus($data)) {
            $data = $this->processRequest($url);
            if (!$this->checkResponseStatus($data)) {
                throw new ServiceProviderException('GoogleDistanceMatrix returned an unknown_error after retry');
            }
        }

        if (!array_key_exists('rows', $data)) {
            throw new ServiceProviderException('Missing key "rows" on data: ' . print_r($data, true));
        }

        return new DistanceMatrixResponse(
            $data['rows'],
            explode('|', $formattedOrigins),
            explode('|', $formattedDestinations),
            $data['origin_addresses'],
            $data['destination_addresses']
        );
    }
public function calculateDistanceMatrix ( array $ origins , array $ destinations ) : DistanceMatrixResponse { $ formattedOrigins = $ this -> formatEntities ( $ origins ) ; $ formattedDestinations = $ this -> formatEntities ( $ destinations ) ; $ query = http_build_query ( [ 'origins' => $ formattedOrigins , 'destinations' => $ formattedDestinations , 'key' => $ this -> apiKey , ] ) ; $ url = $ this -> generateBaseUrl ( ) . '?' . $ query ; $ data = $ this -> processRequest ( $ url ) ; if ( ! $ this -> checkResponseStatus ( $ data ) ) { $ data = $ this -> processRequest ( $ url ) ; if ( ! $ this -> checkResponseStatus ( $ data ) ) { throw new ServiceProviderException ( 'GoogleDistanceMatrix returned an unknown_error after retry' ) ; } } if ( ! array_key_exists ( 'rows' , $ data ) ) { throw new ServiceProviderException ( 'Missing key "rows" on data: ' . print_r ( $ data , true ) ) ; } return new DistanceMatrixResponse ( $ data [ 'rows' ] , explode ( '|' , $ formattedOrigins ) , explode ( '|' , $ formattedDestinations ) , $ data [ 'origin_addresses' ] , $ data [ 'destination_addresses' ] ) ; }
Calculates the distance between multiple origins and destinations

@param DistanceMatrixLocation[] $origins
@param DistanceMatrixLocation[] $destinations

As Google always return the distance value in meters (not km), the function multiplies
the result by 0.62 (km:mile) to calculate an approximation in imperial.
Obs: Be aware that 1 Km = 0.62 miles but 1 meter != 0.62 yards.

@return DistanceMatrixResponse
@throws ServiceProviderException
Calculates the distance between multiple origins and destinations