26th Aug '22
/
0 comments

Google Maps JavaScript API in Bricks

This Pro tutorial provides the steps to show post-specific maps with custom marker content in Bricks using Google's Maps JavaScript API and Advanced Custom Fields.

We are going to

  • use ACF's "Google Map" type of field for being able to specify the address/location for each post.
  • use a "Wysiwyg Editor" custom field for setting custom marker content for each post's location. This will be shown in the info window that appears when any marker is clicked.
  • show the name and address if marker content is empty.
  • optionally set custom marker image.

While it is possible to use the built-in Map element, using the Google's Maps API directly gives us more control for customization incl. being able to set the initial zoom of each post's map with no maximum allowed zoom level unlike the Map element in Bricks 1.5.

Step 1

Paste your Google Maps API key at Bricks → Settings → API Keys.

This is needed to have the map showing the entered location when populating the Google Map field in the post editor.

Step 2

Create a field group having a location and marker_content fields and attach it to your desired post type.

The initial zoom level can be changed here if desired from the default value of 14. This can be set/changed anytime for each post's map by clicking the + and - buttons.

Step 3

Add/edit your posts and for each, enter the address for the Location.

Zoom in/out as needed.

If Marker Content is left empty, we will show the Name and Address from the entered Location automatically in the marker's info window (popup).

Step 4

Edit the child theme's functions.php.

If you've already added other custom code in this file especially in the function hooked to wp_enqueue_scripts action, take a backup/copy the code and re-add it after this step.

Replace

/** * Register/enqueue custom scripts and styles */ add_action( 'wp_enqueue_scripts', function() { // Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder) if ( ! bricks_is_builder_main() ) { wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) ); } } );

with

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Accessing ACF Repeater Sub Fields Programmatically in Bricks Query Loop

Accessing ACF Repeater Sub Fields Programmatically in Bricks Query Loop

It is possible to output sub field's values when a Bricks query loop's type has been set to a ACF Repeater without writing code. This…
Categories:
Pro
Related Services grouped by Service Categories on Single Branch Posts in Bricks

Related Services grouped by Service Categories on Single Branch Posts in Bricks

How to query ACF relationship fields on single CPT templates.
Categories:
Pro
Testimonials Slider in Bricks

Testimonials Slider in Bricks

Setting up a dynamic testimonial slide that pulls the data from a CPT's post fields and custom fields.
Categories:
Tags:
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
Pro
Related Posts using ACF Post Object in Bricks

Related Posts using ACF Post Object in Bricks

Modifying the query loop to limit the posts to only related posts via an ACF Post Object custom field.
Categories:
Tags:
Pro
Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Bricks lacks built-in dynamic tags for ACF field labels, prefixes, and suffixes. This tutorial shows how to add them using custom dynamic tags.
Categories:
Pro
Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

This Pro tutorial shows how we can prefilter the results of a query output by a Bricks query loop to only those for which the…
Categories:
Tags:
Pro
Bricks Slider Images From ACF Flexible Content Sub Field

Bricks Slider Images From ACF Flexible Content Sub Field

Consider the following field group associated with Pages when using ACF Pro: "Page Fields" field group|_"Page Content" Flexible Content field|__"Layout 1" layout|___"Title" Text field|___"Layout 1…