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
DynamicMap
object.
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
DynamicMap
object.
# 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
DynamicMap
object.
# kml($files, $options = [])
Append one or more KML layers to an existing map object.
Arguments
$files
(mixed)$options
(array) - Optional parameters to configure the KML layers. (see below)
Option | Type | Default | Description |
---|---|---|---|
id | string | "kml-{random}" | Reference point for each KML layer. |
kmlLayerOptions | object | null | Accepts any google.maps.KmlLayerOptions (opens new window) properties. |
Returns
- self - A chainable self-reference to this
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# center($coords)
Re-center the map.
Arguments
$coords
(coords) - A simple key-value set of coordinates.
Returns
- self - A chainable self-reference to this
DynamicMap
object.
# fit()
Zoom map to automatically fit all markers within the viewing area.
Returns
- self - A chainable self-reference to this
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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
DynamicMap
object.
# 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.