1313use Geocoder \Exception \NoResultException ;
1414use Geocoder \Exception \QuotaExceededException ;
1515use Geocoder \Exception \UnsupportedException ;
16+ use Geocoder \Exception \InvalidCredentialsException ;
1617use Geocoder \HttpAdapter \HttpAdapterInterface ;
1718
1819/**
@@ -40,18 +41,25 @@ class GoogleMapsProvider extends AbstractProvider implements LocaleAwareProvider
4041 */
4142 private $ useSsl = false ;
4243
44+ /**
45+ * @var string
46+ */
47+ private $ apiKey = null ;
48+
4349 /**
4450 * @param HttpAdapterInterface $adapter An HTTP adapter.
4551 * @param string $locale A locale (optional).
4652 * @param string $region Region biasing (optional).
4753 * @param bool $useSsl Whether to use an SSL connection (optional)
54+ * @param string $apiKey Google Geocoding API key (optional)
4855 */
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 )
5057 {
5158 parent ::__construct ($ adapter , $ locale );
5259
5360 $ this ->region = $ region ;
5461 $ this ->useSsl = $ useSsl ;
62+ $ this ->apiKey = $ apiKey ;
5563 }
5664
5765 /**
@@ -104,6 +112,10 @@ protected function buildQuery($query)
104112 $ query = sprintf ('%s®ion=%s ' , $ query , $ this ->getRegion ());
105113 }
106114
115+ if (null !== $ this ->apiKey ) {
116+ $ query = sprintf ('%s&key=%s ' , $ query , $ this ->apiKey );
117+ }
118+
107119 return $ query ;
108120 }
109121
@@ -129,6 +141,10 @@ protected function executeQuery($query)
129141 throw new NoResultException (sprintf ('Could not execute query %s ' , $ query ));
130142 }
131143
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+
132148 // you are over your quota
133149 if ('OVER_QUERY_LIMIT ' === $ json ->status ) {
134150 throw new QuotaExceededException (sprintf ('Daily quota exceeded %s ' , $ query ));
0 commit comments