GithubHelp home page GithubHelp logo

teampickr / php-google-maps-distance-matrix Goto Github PK

View Code? Open in Web Editor NEW
23.0 12.0 16.0 39 KB

PHP Implementation of the Google Maps Distance Matrix API

PHP 99.83% Shell 0.17%
php laravel google-maps google-maps-api distance-matrix hacktoberfest

php-google-maps-distance-matrix's Introduction

PHP Google Maps Distance Matrix API

This is a simple package that allows access to the Google Maps Distance Matrix API using a (mostly) fluent API. There is support for both the Standard License and Premium/Enterprise License types provided by Google.

Installation

Install the package using composer:

$ composer require teampickr/php-google-maps-distance-matrix

Frameworks

At the moment we only have framework compatibility for Laravel. However, we welcome PRs to add further framework specific behavior as long as it doesn't prevent the package working for others, or pull in dependencies that are not optional (suggested).

Laravel

If you are using Laravel then you can use our service provider. If you have Laravel >5.5 then the package will be auto discovered upon install. Else, add the following to your config/app.php file:

<?php

'providers' => [
    ...
    \TeamPickr\DistanceMatrix\Frameworks\Laravel\DistanceMatrixServiceProvider::class,
]

Facades

Personally, I hate facades. However, I know people like them. If you are using Laravel >5.5 then the facade will be automatically discovered. Else, you can add it in your config/app.php file.

<?php

'aliases' => [
    ...
    'DistanceMatrix' => \TeamPickr\DistanceMatrix\Frameworks\Laravel\DistanceMatrix::class,
]

Configuration

First, make sure you have copied the configuration file:

$ php artisan vendor:publish --tag=config

This will make a config/google.php file, this is where your API Key / License information is fetched from. By default we use the .env configuration values to get your API key.

If you have a standard api key all you need to add to your .env is:

GOOGLE_MAPS_KEY=MY-API-KEY

If you are a Premium / Enterprise Google Maps user, and use a encryption key and client ID then you should add the following to your .env:

GOOGLE_LICENSE_TYPE=premium
GOOGLE_MAPS_CLIENT_ID=MY-CLIENT-ID
GOOGLE_MAPS_ENC_KEY=MY-ENCRYPTION-KEY

Please, make sure you don't store your keys in version control!

Usage

License / API Key

Before making requests you need to create your License object. If you are a standard google maps user, then all you will need is your API key, then you can create your license as follows:

$license = new StandardLicense($apiKey);

If you are a Premium / Enterprise google maps user, then you need to create your license as follows:

$license = new PremiumLicense($clientId, $encryptionKey);

Then, you can start making your request:

$request = new DistanceMatrix($license);

// or

$request = DistanceMatrix::license($license);

Basic usage

$response = DistanceMatrix::license($license)
    ->addOrigin('norwich,gb')
    ->addDestination('52.603669, 1.223785')
    ->request();
   
// I want to make the following but of API better,
// as it looks horrible at the moment.
$rows = $response->rows();
$elements = $rows[0]->elements();
$element = $element[0];

$distance = $element->distance();
$distanceText = $element->distanceText();
$duration = $element->duration();
$durationText = $element->durationText();
$durationInTraffic = $element->durationInTraffic();
$durationInTrafficText = $element->durationInTrafficText();

// or

$response->json['destination_addresses'][0];
$response->json['rows'][0]['elements'][0]['distance']['value'];
$response->json['rows'][0]['elements'][0]['duration_in_traffic']['text'];

php-google-maps-distance-matrix's People

Contributors

alextpickr avatar jono20201 avatar medeirozfer avatar smithstephen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-google-maps-distance-matrix's Issues

Access the error message

I had to edit the core code to find the error message. It was returning the general error: Request provided was invalid.

Json response was obvious how to fix once I could see the error.
[
"destination_addresses" => []
"error_message" => "departure_time is in the past. Traffic information is only available for future and current times."
"origin_addresses" => []
"rows" => []
"status" => "INVALID_REQUEST"
]

Update the Guzzle dependency to 7.0

I'm trying to use php-google-maps-distance-matrix in a Laravel 8 app. Laravel requires Guzzle ^7.0.1. The Guzzle interface does not seem to change between versions 6 & 7, hence it should be safe to simple update the dependency requirement. Could you please update the depencies ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.