ssuhat
ssuhat

Reputation: 7656

Laravel geocoder always return empty

General Information

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

Description

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

Answers (4)

xslibx
xslibx

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

Paulo Taylor
Paulo Taylor

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

Delindel
Delindel

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

Mr. Pyramid
Mr. Pyramid

Reputation: 3935

use like this as mentioned in source

$geocodeData = Geocoder::geocode('Los Angeles, CA');
return $geocodeData;

Upvotes: 0

Related Questions