Locations
When working with dynamic maps, you'll need to specify the locations
that you want to appear on the map. You'll most often encounter this parameter when you create a map or add markers to a map.
What are "locations"? They can be any of the following...
- Coordinates (simple JSON)
- Addresses (from an Address field)
- Elements (like Entries, Users, etc)
# Coordinates
The locations
value can be a set of coordinates, or an array of coordinate sets.
// As a single set of coordinates
const locations = {
'lng': -64.7527469,
'lat': 32.3113966
};
// As an array of coordinate sets
const locations = [
{'lng': -115.7930198, 'lat': 37.2430548},
{'lng': -4.4496567, 'lat': 57.3009274}
];
In JavaScript, you may only use Coordinates
If you are working in JavaScript, then your only option is to work with coordinates. It is physically impossible to translate Address Models and Elements into JavaScript.
# Address Models
The locations
value can be set as an individual Address Model, or as an array of Address Models.
# Elements
The locations
value can be set as an individual Element (opens new window), or as an array (or collection) of Elements. This can include any native Element Types (ie: Entries, Categories, Users, etc), as well as any custom Element Types introduced by other plugins or modules.
{# As a single Element #}
{% set locations = entry %}
{# As an array of Elements #}
{% set locations = craft.entries.all() %}
{# As a collection of Elements #}
{% set locations = craft.entries.collect() %}
Elements must have a valid Address field
When using an Element, ensure that it has at least one Address field attached to it. The plugin will automatically render each valid Address field on the map.