Installation
composer require cyberjaw/google-maps-bundle
Ensure compatibility with Symfony ≥ 2.8.
Register Bundle
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2.8/3.x):
CyberJaw\GoogleMapsBundle\GoogleMapsBundle::class => ['all' => true],
Configure API Key
Add to config/packages/google_maps.yaml (Symfony 4+) or config.yml (Symfony 2.8/3.x):
google_maps:
api_key: 'YOUR_API_KEY'
Enable Twig Theme
Add to config/packages/twig.yaml (Symfony 4+) or config.yml:
twig:
form_themes: ['GoogleMapsBundle:Form:google_maps_layout.html.twig']
Run Assets Install
php bin/console assets:install
Basic Form Integration
// src/Form/YourFormType.php
use CyberJaw\GoogleMapsBundle\Form\Type\GoogleMapsType;
class YourFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('location', GoogleMapsType::class);
}
}
Entity Mapping
// src/Entity/YourEntity.php
class YourEntity {
private $latitude;
private $longitude;
private $city;
private $address;
// Add getters/setters
}
Dynamic Field Configuration Customize form types for latitude/longitude:
$builder->add('location', GoogleMapsType::class, [
'lat_type' => NumberType::class,
'lng_type' => NumberType::class,
'options' => ['attr' => ['class' => 'custom-class']]
]);
Data Binding Bind to entity properties:
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entity->setLatitude($form->get('location')->get('latitude')->getData());
// Repeat for longitude, city, address
}
Validation Add constraints to entity properties:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank
* @Assert\Type("numeric")
*/
private $latitude;
Twig Rendering Override default template:
{% extends 'GoogleMapsBundle:Form:google_maps_layout.html.twig' %}
{% block google_maps_widget %}
{{ form_row(form) }}
<div class="custom-marker">{{ form_widget(form.marker) }}</div>
{% endblock %}
config/packages/ for bundle configuration.assets:install is run post-installation..env).API Key Restrictions
# .env
GOOGLE_MAPS_API_KEY=your_key_here
# config/packages/google_maps.yaml
google_maps:
api_key: '%env(GOOGLE_MAPS_API_KEY)%'
Form Theme Loading
form_themes path is correct.bundles.php/AppKernel.php.assets:install).Data Type Mismatches
latitude as float/string).dump($form->get('location')->getData());
Deprecated Symfony Features
# config/packages/twig.yaml
twig:
debug: '%kernel.debug%'
403 errors if key is invalid).Custom Form Types
Extend GoogleMapsType for specialized use cases:
class CustomGoogleMapsType extends GoogleMapsType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'custom_option' => true,
]);
}
}
Template Overrides
Override google_maps_layout.html.twig to modify rendering:
{% extends 'GoogleMapsBundle:Form:google_maps_layout.html.twig' %}
{% block google_maps_script %}
{{ parent() }}
<script>console.log("Custom script");</script>
{% endblock %}
Event Listeners Hook into form events for dynamic behavior:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
if ($data && $data->getCity()) {
$event->getForm()->add('city_readonly', TextType::class, ['disabled' => true]);
}
});
Service Integration Inject the bundle’s services for advanced use:
use CyberJaw\GoogleMapsBundle\Service\GoogleMapsService;
class YourService {
public function __construct(private GoogleMapsService $maps) {}
}
How can I help you explore Laravel packages today?