Using Google Maps API Key Restrictions

Description

You can use your own Google Maps API Key with The Events Calendar to get more out of your venues and maps.

A Google Maps API key, when used a lot and uncontrolled, can incur costs. In order to have some control over it you can put some restrictions on the key. However that doesn't always play out well.

The Events Calendar requires two APIs to be enabled: the Maps JavaScript API and the Geocoding API.

(You can find detailed information about setting up your Google Maps API Key in this article.)

These two APIs however cannot work with the same restrictions.

Workaround

The workaround is to use two separate Google Maps API Keys.

Use one Google Maps API Key for JavaScript Map displays (The Events Calendar). Your “Maps JavaScript API” key should select “HTTP referrers (web sites)” restriction.

Use a different API Key for venue geolocation lookups (Events Calendar PRO) with Geocoding API. This key should select “IP addresses (web servers, cron jobs, etc.)” restriction.

This screenshot should also be helpful in the explanation.

Code

Since there is only one field for the API key in the settings panel you will need some code as well. Paste the following into your child theme's functions.php file, or create a separate plugin from it.

The API key that you use for Maps JavaScript API, enter under Events > Settings > APIs tab > Google Maps API key

The API key that you use for Geocoding API, put it in the appropriate place in the code.

Plugins

  • The Events Calendar
  • The Events Calendar PRO

Notes

  • Originally written in 2018
  • Snippet by Cliff Paulick
  • Author: Andras Guseo

Disclaimer

As with all of our recipes, please note that we share this in the hope it will be useful but without any guarantees or commitments. If you wish to use it, it is your responsibility to test it first of all and adapt it to your needs (or find someone who can do so on your behalf). We are unable to provide further support in relation to this recipe.