Closed ghost closed 5 years ago
I fixed it by doing the following:
<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client as GuzzleClient;
use Geocoder\Provider\GoogleMaps;
use Http\Adapter\Guzzle6\Client;
use Geocoder\Query\GeocodeQuery;
$apikey = 'AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s';
$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'Brazil', $apikey);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
$result = $geocoder->geocodeQuery(GeocodeQuery::create('240, Rua Clóvis da Fonseca'));
var_export($result);
?>
Response of the API:
Geocoder\Model\AddressCollection::__set_state(array( 'locations' => array ( 0 => Geocoder\Provider\GoogleMaps\Model\GoogleAddress::__set_state(array( 'id' => 'ChIJYfQbLWSZ7JQRHXNPh43uAuQ', 'locationType' => 'ROOFTOP', 'resultType' => array ( 0 => 'street_address', ), 'formattedAddress' => 'R. Clóvis da Fonseca, 240 - Centro, Apucarana - PR, 86800-110, Brazil', 'streetAddress' => NULL, 'intersection' => NULL, 'political' => 'Brazil', 'colloquialArea' => NULL, 'ward' => NULL, 'neighborhood' => NULL, 'premise' => NULL, 'subpremise' => NULL, 'naturalFeature' => NULL, 'airport' => NULL, 'park' => NULL, 'pointOfInterest' => NULL, 'establishment' => NULL, 'subLocalityLevels' => Geocoder\Model\AdminLevelCollection::__set_state(array( 'adminLevels' => array ( 1 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 1, 'name' => 'Centro', 'code' => 'Centro', )), ), )), 'partialMatch' => false, 'coordinates' => Geocoder\Model\Coordinates::__set_state(array( 'latitude' => -23.5511145, 'longitude' => -51.4571663, )), 'bounds' => Geocoder\Model\Bounds::__set_state(array( 'south' => -23.5524634802915, 'west' => -51.45851528029151, 'north' => -23.5497655197085, 'east' => -51.45581731970851, )), 'streetNumber' => '240', 'streetName' => 'Rua Clóvis da Fonseca', 'subLocality' => 'Centro', 'locality' => NULL, 'postalCode' => '86800-110', 'adminLevels' => Geocoder\Model\AdminLevelCollection::__set_state(array( 'adminLevels' => array ( 1 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 1, 'name' => 'Paraná', 'code' => 'PR', )), 2 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 2, 'name' => 'Apucarana', 'code' => 'Apucarana', )), ), )), 'country' => Geocoder\Model\Country::__set_state(array( 'name' => 'Brazil', 'code' => 'BR', )), 'timezone' => NULL, 'providedBy' => 'google_maps', )), ), ))
This is not a correct way to pass parameters to a function in PHP.
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient,$key ='AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s');
This is the correct way:
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s');
Other remark : I suggest you generate a new API key because everyone knows your API key. Don't publish secret API keys !
Thanks @jbelien, I know, i was in despair because I will use the project in our map display, on my job, I will change the key.
My code:
I tried passing the API key as a parameter but it isn't working.