This module makes it simple to add maps to your website. It ships with out-of-the-box support for Google Maps and OpenStreetMap (Leaflet) and is built around a driver layer, so additional providers (Mapbox, etc.) can be added later without touching the rest of the module.
Provide a latitude and longitude (plus an API key when using Google Maps) to create a new map, then render it anywhere on your site with a single line of code.
If you plan to use Google Maps, obtain your API key from Google before you get started. OpenStreetMap works out of the box with no API key required.
google) and OpenStreetMap (osm) per map.There are two ways to render a map:
renderById($id) β renders a map that was created and stored in the admin panel.render(array $config) β renders an ad-hoc map directly from a configuration array, without touching storage. Useful when another module needs to show a map based on runtime data.Both entry points feed the same renderer and produce identical output.
Once you've created a map, you can render it anywhere in your templates by simply using its ID:
<section class="py-5">
<div class="container">
<?= $map->renderById(1); ?>
</div>
</section>
Pass the configuration directly β no database record required:
// Google Maps example
<?= $map->render([
'driver' => 'google',
'lat' => 40.7128,
'lng' => -74.0060,
'zoom' => 12,
'height' => 400,
'api_key' => 'YOUR_GOOGLE_API_KEY',
'markers' => [
[
'lat' => 40.7128,
'lng' => -74.0060,
'label' => 'A',
'description' => '<p>Hello</p>',
'popup' => true
]
]
]); ?>
// OpenStreetMap example (no API key needed)
<?= $map->render([
'driver' => 'osm',
'type' => 'osm',
'lat' => 40.7128,
'lng' => -74.0060,
'zoom' => 12,
'height' => 400,
'markers' => [
[
'lat' => 40.7128,
'lng' => -74.0060,
'label' => 'A',
'description' => '<p>Hello</p>',
'popup' => true
]
]
]); ?>
All keys are optional except lat, lng, and api_key (for Google Maps). Defaults are applied for the rest (driver = 'google', zoom = 5, height = 500, gesture = 'auto', type = 'roadmap', language = current site language).
| Key | Type | Default | Description |
|---|---|---|---|
lat |
float | 0 |
Initial latitude |
lng |
float | 0 |
Initial longitude |
zoom |
int | 5 |
Initial zoom level |
height |
int | 500 |
Map height in pixels |
api_key |
string | '' |
Credential required by the selected driver (see note below) |
driver |
string | 'google' |
Driver identifier ('google' or 'osm') |
type |
string | 'roadmap' |
Map type (driver-specific: e.g. 'roadmap', 'satellite', 'hybrid', 'terrain' for Google; 'osm' for OpenStreetMap) |
gesture |
string | 'auto' |
Gesture handling mode ('auto', 'greedy', 'cooperative', 'none' for Google; 'auto', 'none' for OpenStreetMap) |
language |
string|null | current site language | Language code |
static |
bool | false |
Disable panning, zooming, and keyboard shortcuts |
routed |
bool | false |
Draw a route through the markers (needs at least 2 markers) |
clustering |
bool | false |
Cluster markers when they are close to each other |
style |
string | '' |
Custom style (JSON-encoded JS array, used by Google Maps) |
id |
string | auto-generated | DOM id of the container |
markers |
array | [] |
List of markers (see below) |
Marker keys:
| Key | Type | Default | Description |
|---|---|---|---|
lat |
float | 0 |
Marker latitude |
lng |
float | 0 |
Marker longitude |
label |
string | '' |
Marker label (text label on Google Maps; hover tooltip on OpenStreetMap) |
icon |
string | '' |
Custom icon URL |
description |
string | '' |
Info window / popup HTML |
animation |
bool | false |
Enable marker animation (Google Maps) |
draggable |
bool | false |
Allow dragging the marker |
popup |
bool | false |
Open the info window/popup automatically |
api_keyThe api_key field carries the credential required by the selected driver. For Google Maps it's an API key. OpenStreetMap (osm) requires no API key (the field can be left empty in ad-hoc render() calls). Other providers use the same field for whatever credential they require β e.g., Mapbox calls it an access token. When a driver needs more than a single credential, the module will grow a driver-specific config bag rather than overloading api_key.
The module ships with the following drivers:
google β Google Maps (Map\Driver\GoogleDriver): Uses the Google Maps JavaScript API, Google Places address autocomplete in the admin panel, @google/markerclustererplus for clustering, and the Directions API for routed maps.osm β OpenStreetMap (Map\Driver\OsmDriver): Uses Leaflet 1.9.4 with standard OpenStreetMap tiles, Leaflet.markercluster for clustering, and polyline rendering for routed maps.More drivers can be added by implementing Map\Driver\DriverInterface and registering the class in Map\Driver\DriverRegistry. No other file needs to change β the admin form, the service URL, the asset list, the map types, and the gesture list are all sourced from the active driver.