Learn

location

Display maps of data on your site.

For information on how to create location data, see the location fieldtype.

Deprecating entries:map

Before v1.7, using {{ entries:map }} was how you generated a map on your site. This has been deprecated. Replace calls to entries:map with location:map_listing.

There are three changes that you should be aware of when switching your tags:

  • The center_point parameter is now no longer required; you can leave it out and the map will automatically center itself across all of the found markers
  • The contents of the pop-up markers are now everything between the {{ location:map_listing }} and {{ /location:map_listing }} tags; do not use {{ popup_marker }} tags within to define the pop-up template; this was always a bad idea outside the norms of Statamic
  • This tag can be used to find entries, pages, or both (both being the default), to specify one or the other, set the type variable to either entries or pages

Mapping a Listing

Listings can be placed on a map with the {{ location:map_listing }} tag. This tag will let you set a number of parameters to make things exactly as you want them:

  • All of the same parameters as an {{ entries:listing }} tag to define the data to show
  • The locate_with parameter (required) set to the name of the field which holds location information, and content found that does not have this field will be removed from the list
  • The center_point parameter (optional) which will be the center of the map
  • The added type parameter to let you choose a list of entries (entries), pages (pages), or both (all, the default)
  • All of the variables found in _config/bundles/location/location.yaml can also be overridden as parameters per tag

As before, this tag will create a <div class="map"></div> into which the map will be placed. It’s important to set the dimensions of this element in your CSS somewhere. (Usually, setting a height of whatever you want and a width of 100% works well.) Also, you’ll need to place the {{ location:start_maps }} tag on pages that are displaying maps. This tag contains all of the JavaScript needed to start the maps.

Mapping a Single URL

You can now map out a single URL on your site with the {{ location:map_url }} tag. An example of this tag in action looks like this:

{{ location:map_url url="/contact" locate_with="where" open_popup="true" }}
   {{ if no_results }}
      <p>Sorry, couldn’t find a map for this.</p>
   {{ else }}
      <h2>{{ title }}</h2>
   {{ endif }}
{{ /location:map_url }} 

In the above example, we’re pulling location information from the /contact URL’s where variable. We’re checking to see if no_results is set; if it is, then we didn’t find anything mappable and this text will be output instead of a map, otherwise the content of this tag will be used as the marker’s pop-up template. The url parameter is optional, when you don’t specify it the tag will use the current URL (handy for entry-detail-page templates). You still use locate_with to specify the field name that has the location information. Setting open_popup to true will open up the map-marker’s pop-up by default.

In addition to these parameters, you can set any of the settings found in the location configuration file.

This article was last updated on March 30th, 2016. Find an error? Please let us know!