Usage
Creating a Map field
You can create a Map field in the same way you would create any other field in Craft. Simple go to "Settings" -> "Fields" and click the "New Field" button. Fill out the fields required and select "Map" from the "Field Type" select.
You can now configure the initial state of the map and how it appears to the user. Use the Map to select the initial location of the map and the fields below the map to customize the layout and any restrictions.
Displaying the Map
When accessing the Map field in twig you will have access to the following properties:
lat
The latitude of the selected maps locationlng
The longitude of the selected maps locationzoom
The zoom level of the mapaddress
The full address (see Address)parts
The separate parts of the address (see Parts)distance
The distance the location is from your search (only populated when Searching)
Address
The address
comes in two flavours. The first is as a string, and will output the Full Address as it appears in the Map field.
{{ myMapField.address }}
The second is as a function, address([exclude = [] [, glue = '<br/>']])
, which will allow you to output the address in a more formatted way. Both arguments are optional.
exclude
expects an array of Parts that you don't want to appear when outputting the address. Defaults to[]
.glue
is the string that joins the parts together. _Defaults to'<br/>'
.
{{ myMapField.address(['country'], ', ') }}
The example above is outputting the address as a comma-separated string, excluding the country.
Parts
The parts contains the, well, parts that make up the address.
number
The name or number of the locationaddress
The street address of the location (not the full address)city
The city in which the location is situatedpostcode
The postal or zip code of the locationcounty
The county of the locationstate
The state or region of the locationcountry
The locations country
planet
The planet of the locationsystem
The system containing the planet of the locationarm
The galactic arm of the locationgalaxy
The galaxy the arm is attached togroup
The group the locations galaxy belongs tocluster
The galaxy cluster containing the groupsupercluster
The supercluster the galaxy belongs to
{{ myMapField.parts.city }}
You can also access the parts directly from the map with the exception of the address
part, which can be accessed via streetAddress
.
{{ myMapField.streetAddress }}
{{ myMapField.city }}
Searching
When querying elements you can filter them by proximity to a given location. To do so, simply pass an address and radius to the Map field in the element query.
location
An address string, map field, or{ lat: 0, lng: 0 }
object to search by.country
An optional country to restrict the address string to.radius
The radius around the location to get results from. Defaults to50
.unit
The distance unit to use. Can be eithermi
orkm
. Defaults tokm
.
{% set entries = craft.entries.myMapField({
location: 'Maidstone, Kent',
country: 'UK',
radius: 100,
unit: 'mi',
}).all() %}
If you search using this method you will have access to the distance
property in the resulting elements Map fields. This will return the distance that location is from the location searched for, in the unit specified when searching.
You can also sort by distance
when searching for an address.
{% set entries = craft.entries.myMapField({
location: { lat: 51.272154, lng: 0.514951 },
}).orderBy('distance').all() %}
{% set entries = craft.entries.myMapField({
location: myOtherMapField,
}).orderBy('distance desc').all() %}