Installation:
composer require arcasolutions/google-map-bundle
Ensure ArcaSolutions\GoogleMapBundle\ArcaSolutionsGoogleMapBundle is enabled in config/bundles.php.
Configuration:
Add Google Maps API key to config/packages/arcasolutions_google_map.yaml:
arcasolutions_google_map:
map:
api_key: "YOUR_API_KEY"
First Use Case: Render a basic map in a Twig template:
{{ render(controller('ArcaSolutionsGoogleMapBundle:Default:map')) }}
Or use the service directly in a controller:
use Ivory\GoogleMap\Map;
use Ivory\GoogleMap\Overlay\Marker;
$map = $this->get('ivory_google_map.map');
$map->addMarker(new Marker('Hello World!', [48.8584, 2.2945]));
return $this->render('map.html.twig', ['map' => $map]);
Dynamic Map Rendering: Build maps programmatically in controllers or services:
$map = $this->get('ivory_google_map.map');
$map->setCenter([48.8584, 2.2945])->setZoom(12);
$map->addMarker(new Marker('Location', [48.8584, 2.2945]));
return $this->render('map.html.twig', ['map' => $map]);
Twig Integration: Use Twig extensions for concise syntax:
{% google_map map %}
{% marker 'Location' at [48.8584, 2.2945] %}
{% end_google_map %}
Geocoding & Directions: Leverage services for API calls:
$geocoder = $this->get('ivory_google_map.geocoder');
$result = $geocoder->geocode('1600 Amphitheatre Parkway, Mountain View');
Event Handling: Attach JavaScript events via Twig:
{% google_map map %}
{% marker 'Clickable' at [48.8584, 2.2945] with {
'events': {
'click': 'alert("Marker clicked!")'
}
} %}
{% end_google_map %}
IvoryGoogleMapType for form fields:
$builder->add('location', IvoryGoogleMapType::class, [
'map_options' => ['center' => [48.8584, 2.2945], 'zoom' => 12],
]);
parameters.yaml and reference via %env% for security.Deprecated Bundle:
API Key Restrictions:
InvalidKey errors.Twig Template Caching:
{% block google_map %} in templates to override defaults.Marker Overlays:
$marker = new Marker('Location', [48.8584, 2.2945]);
$marker->setIcon('https://maps.google.com/mapfiles/ms/icons/blue.png');
var_dump($map->getOptions()) to inspect generated JS options.403 errors on /maps/api/js).$this->get('logger')->debug('Map options:', $map->getOptions());
Custom Overlays:
Extend Ivory\GoogleMap\Overlay\OverlayInterface for new shapes (e.g., heatmaps):
class CustomOverlay implements OverlayInterface { ... }
Event Listeners: Subscribe to map events via Symfony’s event dispatcher:
# config/services.yaml
services:
App\EventListener\MapListener:
tags:
- { name: kernel.event_listener, event: ivory_google_map.map_render, method: onMapRender }
API Service Overrides:
Replace default services (e.g., geocoder) with custom implementations:
# config/services.yaml
services:
ivory_google_map.geocoder:
class: App\Service\CustomGeocoder
arguments: ['@http_client']
How can I help you explore Laravel packages today?