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.

{% set map = googleMaps.map(locations) %}

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.
{% do map.markers(locations) %}

# 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.
{% do map.circles(locations) %}

# 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 DynamicMap object.
{% do map.kml(url) %}

# 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 DynamicMap object.
{% do map.geoJson(source) %}

# 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 DynamicMap object.
{% do map.gpx(url) %}

# 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, as north, south, east, and west values.
  • $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 DynamicMap object.
{% do map.groundOverlay(url, bounds) %}

# 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 DynamicMap object.
{% do map.dataset(datasetId) %}

# 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 of COUNTRY, ADMINISTRATIVE_AREA_LEVEL_1, ADMINISTRATIVE_AREA_LEVEL_2, LOCALITY, POSTAL_CODE, or SCHOOL_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 DynamicMap object.
{% do map.boundaries(featureType, placeIds) %}

# 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.
{% do map.styles(styleSet) %}

# zoom($level)

Change the map's zoom level.

Arguments

  • $level (string) - The new zoom level. Must be an integer between 1 - 22.

Returns

  • self - A chainable self-reference to this DynamicMap object.
{% do map.zoom(level) %}

# center($coords)

Re-center the map.

Arguments

Returns

  • self - A chainable self-reference to this DynamicMap object.
{% do map.center(coords) %}

# 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 DynamicMap object.
{% do map.fit() %}

# 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.
{% do map.refresh() %}

# 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.
{% do map.panToMarker(markerId) %}

# 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.
{% do map.setMarkerIcon(markerId, icon) %}

# 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.
{% do map.hideMarker(markerId) %}

# 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.
{% do map.showMarker(markerId) %}

# 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.
{% do map.openInfoWindow(markerId) %}

# 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.
{% do map.closeInfoWindow(markerId) %}

# 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.
{% do map.hideCircle(circleId) %}

# 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.
{% do map.showCircle(circleId) %}

# 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.
{% do map.hideKml(kmlId) %}

# 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.
{% do map.showKml(kmlId) %}

# 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 DynamicMap object.
{% do map.hideGeoJson(geoJsonId) %}

# 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 DynamicMap object.
{% do map.showGeoJson(geoJsonId) %}

# 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 DynamicMap object.
{% do map.hideGpx(gpxId) %}

# 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 DynamicMap object.
{% do map.showGpx(gpxId) %}

# 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 DynamicMap object.
{% do map.hideGroundOverlay(overlayId) %}

# 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 DynamicMap object.
{% do map.showGroundOverlay(overlayId) %}

# 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 DynamicMap object.
{% do map.hideDataset(datasetId) %}

# 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 DynamicMap object.
{% do map.showDataset(datasetId) %}

# 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 DynamicMap object.
{% do map.hideBoundaries(boundariesId) %}

# 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 DynamicMap object.
{% do map.showBoundaries(boundariesId) %}

# 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.
{{ map.tag() }}

If you need to disable the automatic map initialization:

{{ map.tag({'init': false}) }}

If you need to change the map language and/or region:

{{ map.tag({
    'params': {
        'language': 'ja',
        'region': 'JP'
    }
}) }}

# 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.
{% set dna = map.dna %}
{% set dna = map.getDna() %}