Kyle
Kyle

Reputation: 135

Laravel googlemapper package, Cannot get variables from the config file, "Google maps API key is required." ERROR

I was using your plugin these days trying to implement a googlemap on my site. I followed all installation steps and set a variety of variables. I am on laravel 5.4.

I stuck in this error: "Google maps API key is required.". Even I try to hard set the API in your core function, then it throws another error says "region is required". seems like the config file cannot be reached. I also tried to fix the file permissions, but unfortunately that's not the issue. Do you have any ideas about this? This is part of my controller file

use Cornford\Googlmapper\Mapper;
use Cornford\Googlmapper\Facades\MapperFacade;
use Illuminate\Support\Facades\Route;

public function index(Mapper $mapper)
{
    ............
    $firstMapItem = reset($mapArrays);
    $mapper->map(
    $firstMapItem->latitude, $firstMapItem->longitude,
    [
        'zoom' => 10,
        'markers' => ['title' => 'My Location', 'animation' => 'DROP'],
        'clusters' => ['size' => 10, 'center' => true, 'zoom' => 20]
    ]
    );

    // Add information window for each address
    foreach ($mapArrays as $mapArray) {
        $mapper->marker($mapArray->latitude, $mapArray->longitude);
    }
}

Route

Route::get( '/map', 'MapController@index');

view

<div style="width: 100%; height: auto;">
    {!! Mapper::render() !!}
</div>

BTW, I already changed the API value there, my problem is this API value has not been referenced. But if I move the Mapper function from the controller to the Route file, everything is fine. But if I do what I did as above, the API value from the googlmapper.php file isn't referenced.

Upvotes: 1

Views: 1438

Answers (3)

Kyle
Kyle

Reputation: 135

Finally, make it works. Not because of the API issue, the API has been correctly placed. change

use Cornford\Googlmapper\Mapper;

to

use Mapper;

Very simply Facades things I think. credit to @zubinkadva on GitHub. Also the package author @bradcornford also mentioned

"however for reference you can also use the following:

use Cornford\Googlmapper\Facades\MapperFacade as Mapper; "

Upvotes: 1

As Docs said you have to add this

GOOGLE_API_KEY="your google api key here"

in your .env file in the root of your project to get this key you have to go in Google Console

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

From the package docs:

You also need to set your Google API Key into the GOOGLE_API_KEY environment variable.

So, get the API key first here.

Then add this line in the .env file in Laravel project root:

GOOGLE_API_KEY=your_real_api_key_here

Upvotes: 0

Related Questions