Skip to content

Commit 5d18110

Browse files
authored
Merge pull request #1 from langleyfoxall/feature/driving-directions
Directions functionality
2 parents fddb8ca + 4da2b29 commit 5d18110

16 files changed

+640
-399
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor/
22
composer.lock
33
/cache/
4-
.idea
4+
.idea
5+
out.txt

.styleci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
preset: psr2
2+
3+
risky: false
4+
5+
enabled:
6+
- no_blank_lines_before_namespace

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PHP Simple Google Maps
22

3+
[![StyleCI](https://github.styleci.io/repos/159809368/shield?branch=master)](https://github.styleci.io/repos/159809368)
4+
35
This package provides a simple PHP client for various Google Maps APIs.
46

57
## Installation
@@ -67,3 +69,63 @@ $address = $simpleGoogleMaps->reverseGeocode(new LatLong(51.5033635, -0.1276248)
6769
This method will return a string containing the address found at the specified coordinates. If no address
6870
could be found, `null` will be returned.
6971

72+
### Directions
73+
74+
To find the directions between two points, use the `directions` method. The methods expects
75+
three parameters, the origin, the destination, and optionally the travel mode as defined by the
76+
`TravelMode` enum.
77+
78+
See the example usage below.
79+
80+
```php
81+
$address1 = "10 Downing St, Westminster, London SW1A UK";
82+
$address2 = "Schott House, Drummond Rd, Stafford ST16 3EL";
83+
84+
$journey = $simpleGoogleMaps->directions($address1, $address2, TravelMode::DRIVING);
85+
86+
foreach($journey as $step) {
87+
echo $step->duration.' secs ';
88+
echo "\t";
89+
echo $step->distance.' m ';
90+
echo "\t";
91+
echo $step->description;
92+
echo PHP_EOL;
93+
}
94+
95+
echo 'Totals: '.$journey->duration().' secs, '.$journey->distance().' km';
96+
echo PHP_EOL;
97+
```
98+
99+
This will produce output similar to the following.
100+
101+
```
102+
134 secs 452 m Head north on Whitehall / A3212 toward Horse Guards Ave May be closed at certain times or days
103+
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
104+
121 secs 688 m Turn right onto Constitution Hill
105+
34 secs 141 m Turn left onto Duke of Wellington Pl Leaving toll zone
106+
20 secs 83 m Turn right onto Grosvenor Pl
107+
21 secs 107 m Slight right onto Piccadilly May be closed at certain times or days
108+
164 secs 1244 m Slight left onto Park Ln / A4202
109+
35 secs 199 m Slight left onto Cumberland Gate
110+
16 secs 68 m Turn right onto Bayswater Rd
111+
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
112+
48 secs 177 m Turn right onto George St Entering toll zone
113+
152 secs 531 m Turn left onto Seymour Pl
114+
46 secs 231 m Turn left onto Marylebone Rd / A501 Leaving toll zone
115+
722 secs 9209 m Keep right to continue on Marylebone Flyover / A40 Continue to follow A40
116+
708 secs 14581 m Keep right to continue on Western Ave / A40
117+
4636 secs 140047 m Keep right to continue on M40 , follow signs for M25 / Birmingham / Oxford / Beaconsfield
118+
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.
119+
856 secs 22091 m Merge onto M42
120+
14 secs 375 m Keep right at the fork to continue on M6 Toll
121+
1121 secs 33363 m Keep right at the fork to stay on M6 Toll Toll road
122+
756 secs 19905 m Continue onto M6
123+
23 secs 206 m At junction 14 , take the A34 exit to Stone / Stafford (N)
124+
103 secs 1213 m At the roundabout, take the 3rd exit onto A34 Go through 1 roundabout
125+
80 secs 925 m At the roundabout, take the 2nd exit onto Beaconside / A513
126+
128 secs 1550 m Turn right onto Common Rd
127+
13 secs 123 m Turn left onto Astonfields Rd
128+
76 secs 395 m Turn left onto Drummond Rd Destination will be on the left
129+
Totals: 10371 secs, 250507 km
130+
```
131+

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"guzzlehttp/guzzle": "^6.2",
66
"DivineOmega/DO-File-Cache": "^2.0",
77
"divineomega/php-distance": "^1.0",
8+
"illuminate/support": "^5.1",
89
"ext-json": "*",
910
"php": ">=7.0"
1011
},
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
<?php
2-
namespace LangleyFoxall\SimpleGoogleMaps\Factories;
3-
4-
use LangleyFoxall\SimpleGoogleMaps\Objects\SimpleGoogleMaps;
5-
6-
abstract class SimpleGoogleMapsFactory
7-
{
8-
public static function getByKey($key)
9-
{
10-
return new SimpleGoogleMaps($key,null,null);
11-
}
12-
13-
public static function getByClientNameAndCryptKey($clientName,$cryptKey)
14-
{
15-
return new SimpleGoogleMaps(null,$clientName,$cryptKey);
16-
}
17-
}
18-
19-
20-
21-
?>
1+
<?php
2+
namespace LangleyFoxall\SimpleGoogleMaps\Factories;
3+
4+
use LangleyFoxall\SimpleGoogleMaps\Objects\SimpleGoogleMaps;
5+
6+
abstract class SimpleGoogleMapsFactory
7+
{
8+
public static function getByKey($key)
9+
{
10+
return new SimpleGoogleMaps($key, null, null);
11+
}
12+
13+
public static function getByClientNameAndCryptKey($clientName, $cryptKey)
14+
{
15+
return new SimpleGoogleMaps(null, $clientName, $cryptKey);
16+
}
17+
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
<?php
2-
namespace LangleyFoxall\SimpleGoogleMaps\Interfaces;
3-
4-
5-
interface ApiAuthInterface
6-
{
7-
public function applyToUrl($url);
8-
}
9-
?>
1+
<?php
2+
namespace LangleyFoxall\SimpleGoogleMaps\Interfaces;
3+
4+
interface ApiAuthInterface
5+
{
6+
public function applyToUrl($url);
7+
}

src/Interfaces/CacheDriverInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
interface CacheDriverInterface
55
{
66
public function set($key, $value);
7+
78
public function get($key);
9+
810
public function delete($key);
9-
}
11+
}
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
<?php
2-
namespace LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers;
3-
4-
use LangleyFoxall\SimpleGoogleMaps\Interfaces\ApiAuthInterface;
5-
use Exception;
6-
7-
/**
8-
* Class BasicApiAuthDriver
9-
* @package LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers
10-
*/
11-
class BasicApiAuthDriver implements ApiAuthInterface
12-
{
13-
/**
14-
* @var string API Key
15-
*/
16-
private $key;
17-
18-
/**
19-
* BasicApiAuthDriver constructor.
20-
* @param $key
21-
* @throws Exception
22-
*/
23-
public function __construct($key)
24-
{
25-
if(!$key){
26-
throw new Exception("No key set");
27-
}
28-
$this->key = $key;
29-
}
30-
31-
/**
32-
* @param $url
33-
* @return string
34-
*/
35-
public function applyToUrl($url)
36-
{
37-
$authString= "&key=".$this->key;
38-
$appendedUrl = $url.$authString;
39-
40-
return $appendedUrl;
41-
}
42-
}
43-
44-
?>
1+
<?php
2+
namespace LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers;
3+
4+
use Exception;
5+
use LangleyFoxall\SimpleGoogleMaps\Interfaces\ApiAuthInterface;
6+
7+
/**
8+
* Class BasicApiAuthDriver.
9+
*/
10+
class BasicApiAuthDriver implements ApiAuthInterface
11+
{
12+
/**
13+
* @var string API Key
14+
*/
15+
private $key;
16+
17+
/**
18+
* BasicApiAuthDriver constructor.
19+
*
20+
* @param $key
21+
*
22+
* @throws Exception
23+
*/
24+
public function __construct($key)
25+
{
26+
if (!$key) {
27+
throw new Exception('No key set');
28+
}
29+
$this->key = $key;
30+
}
31+
32+
/**
33+
* @param $url
34+
*
35+
* @return string
36+
*/
37+
public function applyToUrl($url)
38+
{
39+
$authString = '&key='.$this->key;
40+
$appendedUrl = $url.$authString;
41+
42+
return $appendedUrl;
43+
}
44+
}
Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
<?php
2-
namespace LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers;
3-
4-
use LangleyFoxall\SimpleGoogleMaps\Interfaces\ApiAuthInterface;
5-
use Exception;
6-
7-
/**
8-
* Class EnterpriseApiAuthDriver
9-
* @package LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers
10-
*/
11-
class EnterpriseApiAuthDriver implements ApiAuthInterface
12-
{
13-
/**
14-
* @var string API client name
15-
*/
16-
private $clientName;
17-
/**
18-
* @var string API crypt key
19-
*/
20-
private $cryptKey;
21-
22-
/**
23-
* EnterpriseApiAuthDriver constructor.
24-
* @param $clientName
25-
* @param $cryptKey
26-
* @throws Exception
27-
*/
28-
public function __construct($clientName, $cryptKey)
29-
{
30-
if(!$clientName){
31-
throw new Exception("ClientName not set");
32-
}
33-
34-
if(!$cryptKey){
35-
throw new Exception("CryptKey not set");
36-
}
37-
$this->clientName = $clientName;
38-
$this->cryptKey = $cryptKey;
39-
}
40-
41-
/**
42-
* @param $url
43-
* @return string
44-
*/
45-
public function applyToUrl($url)
46-
{
47-
$urlWithClient = $url."&client=".$this->clientName;
48-
49-
$parsedUrl = parse_url($urlWithClient);
50-
51-
$urlToEncode = $parsedUrl['path'].'?'.$parsedUrl['query'];
52-
$baseKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->cryptKey));
53-
54-
$signature = hash_hmac('sha1', $urlToEncode, $baseKey, true);
55-
56-
$encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
57-
58-
$appendedUrl = $urlWithClient."&signature=".$encodedSignature;
59-
60-
return $appendedUrl;
61-
}
62-
}
63-
64-
?>
1+
<?php
2+
namespace LangleyFoxall\SimpleGoogleMaps\Objects\ApiAuthDrivers;
3+
4+
use Exception;
5+
use LangleyFoxall\SimpleGoogleMaps\Interfaces\ApiAuthInterface;
6+
7+
/**
8+
* Class EnterpriseApiAuthDriver.
9+
*/
10+
class EnterpriseApiAuthDriver implements ApiAuthInterface
11+
{
12+
/**
13+
* @var string API client name
14+
*/
15+
private $clientName;
16+
/**
17+
* @var string API crypt key
18+
*/
19+
private $cryptKey;
20+
21+
/**
22+
* EnterpriseApiAuthDriver constructor.
23+
*
24+
* @param $clientName
25+
* @param $cryptKey
26+
*
27+
* @throws Exception
28+
*/
29+
public function __construct($clientName, $cryptKey)
30+
{
31+
if (!$clientName) {
32+
throw new Exception('ClientName not set');
33+
}
34+
35+
if (!$cryptKey) {
36+
throw new Exception('CryptKey not set');
37+
}
38+
$this->clientName = $clientName;
39+
$this->cryptKey = $cryptKey;
40+
}
41+
42+
/**
43+
* @param $url
44+
*
45+
* @return string
46+
*/
47+
public function applyToUrl($url)
48+
{
49+
$urlWithClient = $url.'&client='.$this->clientName;
50+
51+
$parsedUrl = parse_url($urlWithClient);
52+
53+
$urlToEncode = $parsedUrl['path'].'?'.$parsedUrl['query'];
54+
$baseKey = base64_decode(str_replace(['-', '_'], ['+', '/'], $this->cryptKey));
55+
56+
$signature = hash_hmac('sha1', $urlToEncode, $baseKey, true);
57+
58+
$encodedSignature = str_replace(['+', '/'], ['-', '_'], base64_encode($signature));
59+
60+
$appendedUrl = $urlWithClient.'&signature='.$encodedSignature;
61+
62+
return $appendedUrl;
63+
}
64+
}

0 commit comments

Comments
 (0)