bastien-wink/google-map-form-type-bundle
Installation
Add the bundle to composer.json:
"require": {
"oh/google-map-form-type-bundle": "dev-master"
}
Run composer update oh/google-map-form-type-bundle.
Register the Bundle
Add to AppKernel.php:
new Oh\GoogleMapFormTypeBundle\OhGoogleMapFormTypeBundle(),
Assetic Configuration
Ensure OhGoogleMapFormTypeBundle is included in assetic.bundles in config.yml:
assetic:
bundles: ['OhGoogleMapFormTypeBundle']
First Use Case
Define a form type with the GoogleMapType:
use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;
$builder->add('location', GoogleMapType::class);
Basic Usage
Use the GoogleMapType in your form builder:
$builder->add('latlng', GoogleMapType::class, [
'label' => 'Select Location',
'mapped' => true,
'required' => true,
'data_class' => null, // Set to your entity if using mapped forms
]);
Customizing Map Options Pass configuration options to tailor the map:
$builder->add('latlng', GoogleMapType::class, [
'options' => [
'center' => [48.8566, 2.3522], // Default center (Paris)
'zoom' => 12,
'disableDefaultUI' => true,
],
]);
Handling Latitude/Longitude in Entities
Ensure your entity has lat and lng properties (or an array latlng):
class Location
{
private $lat;
private $lng;
public function setLatLng(array $latlng)
{
$this->lat = $latlng['lat'];
$this->lng = $latlng['lng'];
}
public function getLatLng()
{
return [$this->lat, $this->lng];
}
}
Validation Use the bundle’s constraints for validation:
use Oh\GoogleMapFormTypeBundle\Validator\Constraints as OhAssert;
/**
* @Assert\Valid()
* @OhAssert\ValidLatLng()
*/
private $latlng;
Edit Existing Locations Pre-populate the map with existing coordinates:
$form = $this->createForm(LocationType::class, $locationEntity);
The bundle automatically binds lat/lng to the latlng field.
Search and Pin Interaction Users can:
latlng).latlng in real-time).Submitting Forms
The latlng field submits as an array ['lat' => ..., 'lng' => ...], which your entity’s setLatLng() method processes.
Outdated Dependencies
Assetic Requirement
// resources/js/app.js
require('oh-google-map-form-type-bundle');
Google Maps API Key
'options' => [
'key' => 'YOUR_GOOGLE_MAPS_API_KEY',
],
Entity Mapping Quirks
mapped: true, ensure your entity’s setLatLng() accepts an array (['lat' => ..., 'lng' => ...]).public function setLatLng(array $latlng)
{
$this->latitude = $latlng['lat'];
$this->longitude = $latlng['lng'];
}
JavaScript Conflicts
// In your template
{{ OhGoogleMapFormTypeBundle::includeJavascripts() }}
Check Console for Errors
F12) to verify Google Maps API loads without 403/404 errors.Validate API Key
Inspect Form Data
dd($request->request->get('latlng')) to debug submitted data.Override Templates
{# templates/OhGoogleMapFormTypeBundle/fields.html.twig #}
{% extends 'OhGoogleMapFormTypeBundle:Form:fields.html.twig' %}
{% block google_map_options %}
{{ parent() }}
{{ form_widget(form.vat) }} {# Add custom fields #}
{% endblock %}
Custom Map Styling
Extend the options array to include map styles:
'options' => [
'styles' => [
{ 'featureType': 'poi', 'stylers': [{ 'visibility': 'off' }] }
],
],
Add Markers Programmatically Use JavaScript events to add markers after initialization:
document.addEventListener('DOMContentLoaded', function() {
const map = document.getElementById('map-canvas').googleMap;
map.addMarker({ lat: 40.7128, lng: -74.0060 });
});
Laravel Integration
namespace App\Services;
use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;
class GoogleMapService {
public function createMapField($name, array $options = [])
{
return new GoogleMapType($options);
}
}
Blade::directive('googleMap', function ($expression) {
return "<?php echo \$this->createFormBuilder()->add($expression)->createView(); ?>";
});
Usage:
@googleMap('latlng', ['options' => ['center' => [48.8566, 2.3522]]])
How can I help you explore Laravel packages today?