Reputation: 7656
Package: https://github.com/geocoder-php/GeocoderLaravel
Geocoder Laravel Version: ^4.0
Laravel Version: 5.5.11
PHP Version: 7.2
Operating System and Version: Windows 10
I have this simple code to try out this package:
return app('geocoder')->geocode('Los Angeles, CA')->get();
But everytime I run it always return me
[{},{},{},{},{}]
this is my geocoder.php:
return [
'cache-duration' => 0,
'providers' => [
Chain::class => [
GoogleMaps::class => [
'en-US',
env('GOOGLE_MAPS_API_KEY'),
],
],
GoogleMaps::class => [
'us',
env('GOOGLE_MAPS_API_KEY'),
],
],
'adapter' => Client::class,
];
All GOOGLE_MAPS_API_KEY
already setup. If I run dd(app('geocoder')->geocode('Los Angeles, CA')->get())
. I can see the results but when I put return
it return me empty.
Upvotes: 1
Views: 1375
Reputation: 4349
In my case, it turns out I was using an API key the was restricted for HTTP. Because php is server side, you have to use an API key that is restricted for IP (not HTTP, that would be for client side, ie. google maps). You can do this in google cloud console by creating a new API KEY that you will use for server side requests. (map a different key to use for google maps / client side requests)
Upvotes: 1
Reputation: 744
For everyone out there that is also desperate about this, if you're experiencing this issue make sure that you have the GeoCoding API enabled on your Google project.
I'd expect that the library would provide feedback about this situation but I was wrong, so if you're experiencing this behavior this might be the answer to your problem
Upvotes: 1
Reputation: 1
Im sure you already found a solution, but just to add up, you are missing the ENV variable for locale:
return [
'cache-duration' => 0,
'providers' => [
Chain::class => [
GoogleMaps::class => [
env('en', 'en-US'), //YOU HAD ONLY 'en-US' here.
env('GOOGLE_MAPS_API_KEY'),
],
],
GoogleMaps::class => [
env('en', 'en-US'), //YOU HAD ONLY 'us' here.
env('GOOGLE_MAPS_API_KEY'),
],
],
'adapter' => Client::class,
];
Hope this helps anyone looking on how to solve this error!
Upvotes: 0
Reputation: 3935
use like this as mentioned in source
$geocodeData = Geocoder::geocode('Los Angeles, CA');
return $geocodeData;
Upvotes: 0