Dynamic Map Model
The Dynamic Map Model is critical for generating a Dynamic Map. Thanks to the magic of chaining, it allows you to build maps that are as complex as they need to be.
# Public Properties
# id
string - The map's unique ID. Can be set manually or generated automatically.
# Public Methods
# __construct($locations = [], $options = [])
This method will be called when a new DynamicMap is initialized. It creates a starting point which sets the map-building chain in motion. You will be able to build upon the map by adding markers, KML layers, etc.
The "map" variable
For each of the remaining examples on this page, the map variable will be an instance of a Dynamic Map Model. In each example, you will see how map methods can be chained at will.
It will be assumed that the map object has already been initialized, as demonstrated above.
Once you have the map object in hand, you can then chain other methods to further customize the map. There is no limit as to how many methods you can chain, nor what order they should be in.
Arguments
$locations(mixed) - See a description of acceptable locations...$options(array) - Optional parameters to configure the map. (see below)
# Dynamic Map Options
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "map-{random}" | Set the id attribute of the map container. |
width | int | null | Set the width of the map (in px). |
height | int | null | Set the height of the map (in px). |
zoom | int | via fitBounds | Set the default zoom level of the map. (1-22) |
center | coords | via fitBounds | Set the center position of the map. |
styles | array | null | An array of map styles. |
cluster | bool|array | false | Enable marker clustering. |
mapOptions | object | null | Accepts any google.maps.MapOptions (opens new window) properties. |
markerOptions | object | null | Accepts any google.maps.MarkerOptions (opens new window) properties. |
infoWindowOptions | object | null | Accepts any google.maps.InfoWindowOptions (opens new window) properties. |
infoWindowTemplate | string | null | Template path to use for creating info windows. |
infoWindowData | array | null | Additional data for passing into an info window. |
markerLink | string | null | URL to go to when each marker is clicked. |
markerClick | string | null | JS callback function triggered when each marker is clicked. |
field | string|array | null | Address field(s) to be included on the map. (includes all by default) |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
Locations are Skippable
If you skip the locations parameter, a blank map will be created.
# markers($locations, $options = [])
Append markers to an existing map object.
Arguments
$locations(mixed) - See a description of acceptable locations...$options(array) - Optional parameters to configure the markers. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "marker-{random}" | Reference point for each marker. |
icon | object|string | null | An icon as defined by google.maps.MarkerOptions (opens new window). |
markerOptions | object | null | Accepts any google.maps.MarkerOptions (opens new window) properties. |
infoWindowOptions | object | null | Accepts any google.maps.InfoWindowOptions (opens new window) properties. |
infoWindowTemplate | string | null | Template path to use for creating info windows. |
infoWindowData | array | null | Additional data for passing into an info window. |
markerLink | string | null | URL to go to when each marker is clicked. |
markerClick | string | null | JS callback function triggered when each marker is clicked. |
field | string|array | null | Address field(s) to be included on the map. (includes all by default) |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# circles($locations, $options = [])
Add one or more circles to an existing map object.
Arguments
$locations(mixed) - See a description of acceptable locations...$options(array) - Optional parameters to configure circles. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "circle-{random}" | Reference point for each circle. |
circleOptions | object | null | Accepts any google.maps.CircleOptions (opens new window) properties. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# kml($url, $options = [])
Append a KML layer to an existing map object.
Arguments
$url(string) - URL of the KML or KMZ file.$options(array) - Optional parameters to configure the KML layer. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "kml-{random}" | Reference point for each KML layer. |
preserveViewport | bool | false | If true, the map will not zoom to fit the layer once it loads. |
suppressInfoWindows | bool | false | If true, clicking a shape will not open an info window. |
clickable | bool | true | If false, the layer will ignore clicks entirely. |
zIndex | int | null | Stacking order of the layer, relative to other layers. |
style | array | null | Any google.maps.Data.StyleOptions (opens new window) properties. Overrides the styles from the KML file. |
kmlLayerOptions | array | null | Any of the options above, nested. If an option is set both ways, the nested value wins. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# geoJson($source, $options = [])
Append a GeoJSON layer to an existing map object.
Arguments
$source(string|array) - URL of a GeoJSON file, a JSON string, or the GeoJSON data itself.$options(array) - Optional parameters to configure the GeoJSON layer. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "geojson-{random}" | Reference point for each GeoJSON layer. |
preserveViewport | bool | false | If true, the map will not zoom to fit the layer once it loads. |
suppressInfoWindows | bool | false | If true, clicking a shape will not open an info window. |
clickable | bool | true | If false, the layer will ignore clicks entirely. |
zIndex | int | null | Stacking order of the layer, relative to other layers. |
style | array | null | Any google.maps.Data.StyleOptions (opens new window) properties. Overrides the styles from the GeoJSON data. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# gpx($url, $options = [])
Append a GPX layer (tracks, routes, and waypoints) to an existing map object.
Arguments
$url(string) - URL of the GPX file.$options(array) - Optional parameters to configure the GPX layer. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "gpx-{random}" | Reference point for each GPX layer. |
preserveViewport | bool | false | If true, the map will not zoom to fit the layer once it loads. |
suppressInfoWindows | bool | false | If true, clicking a track or waypoint will not open an info window. |
clickable | bool | true | If false, the layer will ignore clicks entirely. |
zIndex | int | null | Stacking order of the layer, relative to other layers. |
style | array | null | Any google.maps.Data.StyleOptions (opens new window) properties. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# groundOverlay($url, $bounds, $options = [])
Place an image onto an existing map object, stretched to fit the specified bounds.
Arguments
$url(string) - URL of the image.$bounds(array) - The edges of the image, asnorth,south,east, andwestvalues.$options(array) - Optional parameters to configure the ground overlay. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | "ground-overlay-{random}" | Reference point for each ground overlay. |
opacity | float | 1 | Opacity of the image, from 0 (invisible) to 1 (solid). |
clickable | bool | true | If false, the image will ignore clicks. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# dataset($datasetId, $options = [])
Show a dataset which has been uploaded to Google Cloud. Requires a vector Map ID.
Arguments
$datasetId(string) - The ID of the dataset in Google Cloud.$options(array) - Optional parameters to configure the dataset. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | (the dataset ID) | Reference point for the dataset. |
style | array | (blue) | Any google.maps.FeatureStyleOptions (opens new window) properties. |
suppressInfoWindows | bool | false | If true, clicking a shape will not open an info window. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# boundaries($featureType, $placeIds, $options = [])
Shade Google's own boundaries of countries, states, cities, and more. Requires a vector Map ID.
Arguments
$featureType(string) - The type of boundary. One ofCOUNTRY,ADMINISTRATIVE_AREA_LEVEL_1,ADMINISTRATIVE_AREA_LEVEL_2,LOCALITY,POSTAL_CODE, orSCHOOL_DISTRICT.$placeIds(string|array) - A Place ID, or array of Place IDs, of the areas to shade.$options(array) - Optional parameters to configure the boundaries. (see below)
| Option | Type | Default | Description |
|---|---|---|---|
id | string | (the feature type) | Reference point for the boundaries. |
style | array | (blue) | Any google.maps.FeatureStyleOptions (opens new window) properties. |
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# styles($styleSet)
Style a map based on a given array of styles.
Generating Styles
For more information on how to generate a set of styles, read Styling a Map.
Arguments
$styleSet(array) - A set of styles to be applied to the map.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# zoom($level)
Change the map's zoom level.
Arguments
$level(string) - The new zoom level. Must be an integer between1-22.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# center($coords)
Re-center the map.
Arguments
$coords(coords) - A simple key-value set of coordinates.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# fit()
Zoom map to automatically fit all markers and layers within the viewing area.
Which layers are included?
KML, GeoJSON, and GPX layers and ground overlays are included, as long as they are visible and have finished loading. Datasets and boundaries are left out, since Google doesn't provide their bounds.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# refresh()
Refresh an existing map. You may need to do this after the page has been resized, or if something has been moved or changed.
Generally Useless
There probably aren't a lot of good reasons to use this method in Twig or PHP, because it would be fired immediately after the map finishes rendering. It exists mainly for parity between languages.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# panToMarker($markerId)
Re-center map on the specified marker.
Arguments
$markerId(string) - ID of the target marker.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# setMarkerIcon($markerId, $icon)
Set the icon of an existing marker.
Arguments
$markerId(string|array|'*') - A marker ID, array of marker IDs, or'*'for all markers.$icon(string|icon (opens new window)) - URL of marker icon image.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideMarker($markerId)
Hide a marker.
Arguments
$markerId(string|array|'*') - A marker ID, array of marker IDs, or'*'for all markers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showMarker($markerId)
Show a marker.
Arguments
$markerId(string|array|'*') - A marker ID, array of marker IDs, or'*'for all markers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# openInfoWindow($markerId)
Open the info window of a specific marker.
Arguments
$markerId(string|array|'*') - A marker ID, array of marker IDs, or'*'for all info windows.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# closeInfoWindow($markerId)
Close the info window of a specific marker.
Arguments
$markerId(string|array|'*') - A marker ID, array of marker IDs, or'*'for all info windows.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideCircle($circleId)
Hide a circle.
Arguments
$circleId(string|array|'*') - A circle ID, array of circle IDs, or'*'for all circles.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showCircle($circleId)
Show a circle.
Arguments
$circleId(string|array|'*') - A circle ID, array of circle IDs, or'*'for all circles.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideKml($kmlId)
Hide a KML layer.
Arguments
$kmlId(string|array|'*') - A KML layer ID, array of KML layer IDs, or'*'for all KML layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showKml($kmlId)
Show a KML layer.
Arguments
$kmlId(string|array|'*') - A KML layer ID, array of KML layer IDs, or'*'for all KML layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideGeoJson($geoJsonId)
Hide a GeoJSON layer.
Arguments
$geoJsonId(string|array|'*') - A GeoJSON layer ID, array of GeoJSON layer IDs, or'*'for all GeoJSON layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showGeoJson($geoJsonId)
Show a GeoJSON layer.
Arguments
$geoJsonId(string|array|'*') - A GeoJSON layer ID, array of GeoJSON layer IDs, or'*'for all GeoJSON layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideGpx($gpxId)
Hide a GPX layer.
Arguments
$gpxId(string|array|'*') - A GPX layer ID, array of GPX layer IDs, or'*'for all GPX layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showGpx($gpxId)
Show a GPX layer.
Arguments
$gpxId(string|array|'*') - A GPX layer ID, array of GPX layer IDs, or'*'for all GPX layers.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideGroundOverlay($overlayId)
Hide a ground overlay.
Arguments
$overlayId(string|array|'*') - A ground overlay ID, array of ground overlay IDs, or'*'for all ground overlays.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showGroundOverlay($overlayId)
Show a ground overlay.
Arguments
$overlayId(string|array|'*') - A ground overlay ID, array of ground overlay IDs, or'*'for all ground overlays.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideDataset($datasetId)
Hide a dataset.
Arguments
$datasetId(string|array|'*') - A dataset ID, array of dataset IDs, or'*'for all datasets.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showDataset($datasetId)
Show a dataset.
Arguments
$datasetId(string|array|'*') - A dataset ID, array of dataset IDs, or'*'for all datasets.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# hideBoundaries($boundariesId)
Hide boundaries.
Arguments
$boundariesId(string|array|'*') - A boundaries ID, array of boundaries IDs, or'*'for all boundaries.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# showBoundaries($boundariesId)
Show boundaries.
Arguments
$boundariesId(string|array|'*') - A boundaries ID, array of boundaries IDs, or'*'for all boundaries.
Returns
- self - A chainable self-reference to this
DynamicMapobject.
# tag($options = [])
Renders the necessary <div> container to hold the map. The final <div> will contain specific attributes and data, which are then used to generate the map. Each container must be initialized in order for its dynamic map to be created.
Arguments
$options(array) - Configuration options for the rendered<div>.
| Option | Type | Default | Description |
|---|---|---|---|
init | bool | true | Whether to automatically initialize the map via JavaScript. |
assets | bool | true | Whether to preload the necessary JavaScript assets. |
callback | string | null | JavaScript function to run after the map has loaded. |
params | object | {} | Optional parameters (opens new window) for the Google Maps API. |
api | object | {} | [DEPRECATED] Use params instead. |
By setting the init option to false, the map will not be automatically initialized in JavaScript. It must therefore be manually initialized in JavaScript when the page has completely rendered.
Returns
- Markup - A Twig Markup instance, ready to be rendered in Twig with curly braces.
If you need to disable the automatic map initialization:
If you need to change the map language and/or region:
# getDna()
Get the complete map DNA, which is used to hydrate a map's container in the DOM.
Aliased as dna property via magic method.
Returns
- array - An array of data containing the complete map details.