Installation:
composer require alfamegaxq/simple-g-maps
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
SimpleGMapsBundle\SimpleGMapsBundle::class => ['all' => true],
Route Configuration:
Add to config/routes.yaml:
google_autosuggest:
resource: "@SimpleGMapsBundle/Controller/"
type: annotation
Include Dependencies: Ensure your layout includes:
<!-- In <head> -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<!-- Before </body> -->
<script src="{{ asset('bundles/gmaps/js/script.js') }}"></script>
First Use Case: Render a city autocomplete input in a Twig template:
{{ render(controller('SimpleGMapsBundle:Suggest:renderCitySuggest', {
inputParams: 'name="city" class="city-autocomplete" placeholder="Enter city"',
language: 'en',
country: 'us'
})) }}
Dynamic Country/Language Handling: Pass dynamic values via Twig:
{% set country = user.countryCode %}
{{ render(controller('SimpleGMapsBundle:Suggest:renderCitySuggest', {
inputParams: '...',
country: country,
language: country == 'de' ? 'de' : 'en'
})) }}
Integration with Forms: Use with Symfony Forms:
$builder->add('city', TextType::class, [
'attr' => [
'class' => 'city-autocomplete',
'data-country' => $user->getCountryCode()
]
]);
Render the form with the bundle’s JS included.
API-Driven Extensions:
Extend the backend logic by subclassing SimpleGMapsBundle\Controller\SuggestController and overriding renderCitySuggestAction to:
Asset Management: Override the default JS/CSS by:
php bin/console assets:install public
script.js via resources/assets/gmaps/js/script.js (Symfony 4+ asset pipeline).Deprecated Symfony 3.1:
symfony/webpack-encore-bundle for asset management if upgrading to Symfony 4/5.AppKernel or use config/bundles.php with a compatibility layer.Missing Google Maps Logo:
<div class="gmaps-attribution">
<a href="https://www.google.com/maps">Powered by Google</a>
</div>
jQuery UI Dependency:
$ or jQuery.(function($) {
// Bundle's JS code here
})(jQuery);
API Rate Limits:
// In a custom controller extension
$cache = $this->get('doctrine_cache.providers.gmaps');
$cachedData = $cache->fetch('gmaps_cities_' . $country);
Check Network Requests:
F12) → Network tab. Verify the /suggest/city endpoint returns valid JSON.Accept: application/json header. Add to Guzzle client:
$client->getConfig()->set('headers', ['Accept' => 'application/json']);
Log API Errors:
try {
$response = $client->request('GET', $url);
} catch (\GuzzleHttp\Exception\RequestException $e) {
$this->get('logger')->error('GMaps API Error', ['response' => $e->getResponse()->getBody()]);
throw new \RuntimeException('Failed to fetch data from Google Maps API');
}
Validate Input Params:
inputParams Twig variable must be a string. Escape dynamic values:
{% set params = 'name="city" class="city-autocomplete" data-attr="' ~ attribute|e('html_attr') ~ '"' %}
Custom Autocomplete Sources:
SimpleGMapsBundle/Resources/views/Suggest/renderCitySuggest.html.twig to use a different template engine or add markers.Add New Endpoints:
SuggestController and add methods for:
/geocode)./distance)./**
* @Route("/distance", name="gmaps_distance")
*/
public function distanceMatrixAction(Request $request) {
$origin = $request->query->get('origin');
$destination = $request->query->get('destination');
// Use Guzzle to call Google's Distance Matrix API
}
Configuration via Parameters:
config/packages/simple_g_maps.yaml:
simple_g_maps:
default_country: 'us'
api_key: '%env(GOOGLE_MAPS_API_KEY)%'
cache_ttl: 3600 # 1 hour
$this->getParameter('simple_g_maps.default_country').How can I help you explore Laravel packages today?