Skip to main content

Modifying Event Location Google Maps

Events Manager draws its interactive maps with the Google Maps JavaScript API. The plugin prints a placeholder element and some data, and its script turns each one into a map when the page loads. You can step in at three points: before a map is created, to change its options; after it is created, to work with the map object; and in the template that prints the placeholder.

This page is about interactive maps. If your site uses static or embedded maps, see map types. The balloon formats and other map settings are in Events then Settings then Formatting then Maps.

Before you start

Maps must be working, with an API key set up as described in Google Maps. You need somewhere to add JavaScript, such as a script enqueued by your own plugin. The events on this page are in Events Manager 7.5.

The two kinds of map​

MapPrinted byTemplate
A single location's mapThe #_LOCATIONMAP placeholderplaceholders/locationmap.php
A map of many locationsThe [locations_map] and [events_map] shortcodestemplates/map-global.php

JavaScript events​

Events Manager triggers these on document through jQuery. Listen with jQuery(document).on().

EventWhenArguments after the event object
em_maps_location_map_optionsBefore a single-location map is createdmap_options, the object about to be passed to google.maps.Map
em_maps_locations_map_optionsBefore a multi-location map is createdmap_options
em_maps_location_marker_optionsBefore a marker is created, on both kinds of mapmarker_options, the object about to be used for the marker
em_maps_location_hookAfter a single-location map is readymap, infowindow, marker, map_id
em_maps_locations_hookAfter a multi-location map is readymap, data (the locations), map_id, markers
em_maps_locations_hook_not_foundWhen a multi-location map has no locations to showThe map's element

The options events pass an object you change in place. The two _hook events hand you finished Google Maps objects.

em_maps_locations_hook and em_maps_locations_hook_not_found are also dispatched as native CustomEvents on document, with the same values in event.detail (map, data, id, markers, el), for code that doesn't use jQuery.

Steps​

  1. Enqueue a script of your own on the front end, with jquery as a dependency:

    In your plugin
    add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_script( 'my-em-maps', plugins_url( 'my-em-maps.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
    } );
  2. In that script, listen for the event you need. The examples below go in my-em-maps.js.

  3. Reload a page with a map and check the result.

Change the zoom and map type of single-location maps​

my-em-maps.js
jQuery( document ).on( 'em_maps_location_map_options', function ( e, map_options ) {
map_options.zoom = 16;
map_options.mapTypeId = google.maps.MapTypeId.HYBRID;
map_options.mapTypeControl = true;
} );

The defaults are a zoom of 14, the road map, no map type control, and "cooperative" gesture handling, which asks visitors to use two fingers or hold a key to move the map.

Work with the map after it loads​

my-em-maps.js
jQuery( document ).on( 'em_maps_location_hook', function ( e, map, infowindow, marker, map_id ) {
infowindow.close();
map.setOptions( { streetViewControl: false } );
} );

Stop a multi-location map zooming in too far​

A multi-location map fits itself to its markers. With one marker, or several close together, that can zoom in to street level.

my-em-maps.js
jQuery( document ).on( 'em_maps_locations_hook', function ( e, map, data, map_id, markers ) {
google.maps.event.addListenerOnce( map, 'idle', function () {
if ( map.getZoom() > 14 ) {
map.setZoom( 14 );
}
} );
} );

Markers are advanced markers​

Since version 7.2.3, Events Manager creates markers with Google's AdvancedMarkerElement in place of the older google.maps.Marker. The one exception is a map with custom styles applied, which can't host advanced markers and still gets the older kind. Each marker has an em_advanced property that is true for an advanced marker.

Code written for older versions that calls marker.setIcon(), marker.getPosition() or marker.setMap() fails on an advanced marker, which has properties instead, such as marker.position, marker.map and marker.content. Check marker.em_advanced if your code has to handle both. Google's advanced markers documentation lists them.

Change the HTML with templates​

Both maps are printed by template files you can override: placeholders/locationmap.php for #_LOCATIONMAP and templates/map-global.php for the multi-location maps.

Events Manager's script finds maps by the class names and ids in those templates and reads the coordinates and balloon text from the hidden elements next to them. Keep those in place if you want the script to keep building the map. If you remove them, the script ignores the element and you can create the map with your own code.

Test it​

  1. Open a location page that shows a map, with the browser's developer tools open on the Console tab.
  2. Confirm the change (zoom level, map type, closed balloon) and that no errors are logged.
  3. If nothing changes, check that your script is loaded on the page and that the listener is registered before the map is created. A script in the footer with jquery as a dependency is early enough, because maps are created only after the Google Maps API has loaded.