Code Monkey home page Code Monkey logo

php-simple-google-maps's Introduction

PHP Simple Google Maps

StyleCI

This package provides a simple PHP client for various Google Maps APIs.

Installation

To install, just run the following composer command.

composer require langleyfoxall/simple-google-maps

Remember to include the vendor/autoload.php file if your framework does not do this for you.

Usage

To use Simple Google Maps, you must first create a new instance. This can be done is two ways, dependant on whether you have a standard API key, or a clientName and cryptKey (for enterprise / premium plans).

// Standard authentication:
$simpleGoogleMaps = SimpleGoogleMapsFactory::getByKey(getenv('KEY'));

// Enterprise / premium plan authentication:
$simpleGoogleMaps = SimpleGoogleMapsFactory::getByClientNameAndCryptKey(getenv('CLIENT_NAME'), getenv('CRYPT_KEY'));

Geocoding

To convert an address to a set of GPS coordinates, use the geocode method, as shown below.

$latLng = $simpleGoogleMaps->geocode('10 Downing St, Westminster, London SW1A UK');

Optionally, you can allow partial matches to be returned if your input address is not highly accurate. You can do so with the allowPartialMatches method, as shown below.

$latLng = $simpleGoogleMaps->allowPartialMatches()->geocode('test address');

The above method will return a object of type LatLong, which allows you to access the GPS coordinates as shown below.

$latitude = $latLng->lat;
$longitude = $latLng->long;

You can also calculate the distance between two LatLong objects by using the distanceTo method. The distance is returned in kilometers, and takes into account the curvature of the Earth using the Haversine formula.

$distance = $fromCoords->distanceTo($toCoords);

Reverse Geocoding

To lookup an address from a set of GPS coordinate, use the reverseGeocode method, as shown below.

$address = $simpleGoogleMaps->reverseGeocode(new LatLong(51.5033635, -0.1276248));

This method will return a string containing the address found at the specified coordinates. If no address could be found, null will be returned.

Directions

To find the directions between two points, use the directions method. The methods expects three parameters, the origin, the destination, and optionally the travel mode as defined by the TravelMode enum.

See the example usage below.

$address1 = "10 Downing St, Westminster, London SW1A UK";
$address2 = "Schott House, Drummond Rd, Stafford ST16 3EL";

$journey = $simpleGoogleMaps->directions($address1, $address2, TravelMode::DRIVING);

foreach($journey as $step) {
    echo $step->duration.' secs  ';
    echo "\t";
    echo $step->distance.' m    ';
    echo "\t";
    echo $step->description;
    echo PHP_EOL;
}

echo 'Totals: '.$journey->duration().' secs, '.$journey->distance().' km';
echo PHP_EOL;

This will produce output similar to the following.

134 secs        452 m           Head north on Whitehall / A3212 toward Horse Guards Ave May be closed at certain times or days 
203 secs        1029 m          At the roundabout, take the 1st exit onto The Mall Parts of this road may be closed at certain times or days 
121 secs        688 m           Turn right onto Constitution Hill 
34 secs         141 m           Turn left onto Duke of Wellington Pl Leaving toll zone 
20 secs         83 m            Turn right onto Grosvenor Pl 
21 secs         107 m           Slight right onto Piccadilly May be closed at certain times or days 
164 secs        1244 m          Slight left onto Park Ln / A4202 
35 secs         199 m           Slight left onto Cumberland Gate 
16 secs         68 m            Turn right onto Bayswater Rd 
92 secs         410 m           Slight left onto Edgware Rd / A5 Entering toll zone in 280 m at Upper Berkeley St Leaving toll zone in 300 m at Stourcliffe St 
48 secs         177 m           Turn right onto George St Entering toll zone 
152 secs        531 m           Turn left onto Seymour Pl 
46 secs         231 m           Turn left onto Marylebone Rd / A501 Leaving toll zone 
722 secs        9209 m          Keep right to continue on Marylebone Flyover / A40 Continue to follow A40 
708 secs        14581 m         Keep right to continue on Western Ave / A40 
4636 secs       140047 m        Keep right to continue on M40 , follow signs for M25 / Birmingham / Oxford / Beaconsfield 
49 secs         1164 m          At junction 3A , take the M42 / Railway Station / Airport exit to M1 / M6 / Birmingham (E, N & C) / Solihull / N.E.C. 
856 secs        22091 m         Merge onto M42 
14 secs         375 m           Keep right at the fork to continue on M6 Toll 
1121 secs       33363 m         Keep right at the fork to stay on M6 Toll Toll road 
756 secs        19905 m         Continue onto M6 
23 secs         206 m           At junction 14 , take the A34 exit to Stone / Stafford (N) 
103 secs        1213 m          At the roundabout, take the 3rd exit onto A34 Go through 1 roundabout 
80 secs         925 m           At the roundabout, take the 2nd exit onto Beaconside / A513 
128 secs        1550 m          Turn right onto Common Rd 
13 secs         123 m           Turn left onto Astonfields Rd 
76 secs         395 m           Turn left onto Drummond Rd Destination will be on the left 
Totals: 10371 secs, 250507 km

php-simple-google-maps's People

Contributors

divineomega avatar jameswilddev avatar kirsty-gasston avatar kirstyg avatar lfnicklangley avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

divineomega dheia

php-simple-google-maps's Issues

Laravel 8 compatibility

This package is locked to guzzle v 6.x -- Laravel 8 now uses 7.x; there are breaking changes as listed here.

It's also worth noting that guzzle 7.x removes PHP 5 support, and more importantly sets the minimum PHP version to 7.2 -- I have seen dependent packages (laravel-addresses for one) that have the minimum set to 7.1, so upgrading could cause breaking changes for legacy software running on PHP 7.1.

I will look further into this when I get the chance, but I've raised this issue so that it doesn't get forgotten and to welcome input from you other devs.

Add unit tests

Units tests are required to ensure code functions as expected.

Needs Laravel 7 support

This package blocks installation for Laravel 7 projects:

langleyfoxall/simple-google-maps v1.0.0 requires laravel/framework ^5.1||^6.0 -> satisfiable by laravel/framework[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev, 6.x-dev].

This may be as simple as adding ^7.0 to composer.json, but I don't currently have time to test it.

Add additional methods to mutate time & distance

It would be very convenient if you could mutate the time that is calculated, without needing to perform additional processing of the data outside of the package.

Time

If we use the demo as an example, it currently returns the information in seconds. What I propose would do the following.

$journey->duration()->toMinutes()

This would then return, for example, 5 mins. Additional options would also be really handy, e.g. toHours().

Distance

Something similar could also be done to the distance.

$journey->distance()->toMiles()

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.