13
13
use Geocoder \Exception \NoResultException ;
14
14
use Geocoder \Exception \QuotaExceededException ;
15
15
use Geocoder \Exception \UnsupportedException ;
16
+ use Geocoder \Exception \InvalidCredentialsException ;
16
17
use Geocoder \HttpAdapter \HttpAdapterInterface ;
17
18
18
19
/**
@@ -40,18 +41,25 @@ class GoogleMapsProvider extends AbstractProvider implements LocaleAwareProvider
40
41
*/
41
42
private $ useSsl = false ;
42
43
44
+ /**
45
+ * @var string
46
+ */
47
+ private $ apiKey = null ;
48
+
43
49
/**
44
50
* @param HttpAdapterInterface $adapter An HTTP adapter.
45
51
* @param string $locale A locale (optional).
46
52
* @param string $region Region biasing (optional).
47
53
* @param bool $useSsl Whether to use an SSL connection (optional)
54
+ * @param string $apiKey Google Geocoding API key (optional)
48
55
*/
49
- public function __construct (HttpAdapterInterface $ adapter , $ locale = null , $ region = null , $ useSsl = false )
56
+ public function __construct (HttpAdapterInterface $ adapter , $ locale = null , $ region = null , $ useSsl = false , $ apiKey = null )
50
57
{
51
58
parent ::__construct ($ adapter , $ locale );
52
59
53
60
$ this ->region = $ region ;
54
61
$ this ->useSsl = $ useSsl ;
62
+ $ this ->apiKey = $ apiKey ;
55
63
}
56
64
57
65
/**
@@ -104,6 +112,10 @@ protected function buildQuery($query)
104
112
$ query = sprintf ('%s®ion=%s ' , $ query , $ this ->getRegion ());
105
113
}
106
114
115
+ if (null !== $ this ->apiKey ) {
116
+ $ query = sprintf ('%s&key=%s ' , $ query , $ this ->apiKey );
117
+ }
118
+
107
119
return $ query ;
108
120
}
109
121
@@ -129,6 +141,10 @@ protected function executeQuery($query)
129
141
throw new NoResultException (sprintf ('Could not execute query %s ' , $ query ));
130
142
}
131
143
144
+ if ('REQUEST_DENIED ' === $ json ->status && 'The provided API key is invalid. ' === $ json ->error_message ) {
145
+ throw new InvalidCredentialsException (sprintf ('API key is invalid %s ' , $ query ));
146
+ }
147
+
132
148
// you are over your quota
133
149
if ('OVER_QUERY_LIMIT ' === $ json ->status ) {
134
150
throw new QuotaExceededException (sprintf ('Daily quota exceeded %s ' , $ query ));
0 commit comments