Skip to main content

How to Add Google Maps in Drupal (Best Modules)

Category: 
Published: 
Reading time
: 4 min

Maps on websites are a useful addition, allowing users to reach, for example, your company headquarters in a simple way. Depending on the requirements, you can implement them in many different ways. This article will show you how to embed Google Maps in a Drupal website.

Google Maps and Drupal

Google Maps

Google Maps is a service introduced in 2005. Its popularity has grown over the years and it is now used by over a billion people worldwide each month. Apart from the map, it gives users the possibility to:

  • view satellite images,
  • see traffic density
  • check the area’s geographical characteristics,
  • check public transport,
  • find bike paths,
  • see panoramic street views.

The Cost

The service is free for regular users. However, if you are a developer and want to integrate with Google Maps, you must register on the Google Cloud platform obtain API keys.

Each month you have to settle with the service, and the amount depends on the number of queries generated by the website. At the moment, the company offers ~28500 free operations, which is equivalent to $200. Detailed information can be found directly on the manufacturer’s website.

How to add Google Maps in Drupal?

Adding Google Maps to your Drupal website can be achieved by using modules. There are two interesting ones in the library:

  1. Simple Google Maps (~24000 active instances).
  2. Geofield Map (~16000 active instances).

Select the project depending on your needs. Simple Google Maps does not require an API key and service registration due to its limited capabilities. Based on the text field, the module attaches a map to the page indicating a given address. Each user can share the location by placing a map, and the module uses it cleverly.

Simple Google Maps module

If this solution does not satisfy you, you will have to use Geofield Map, which requires access to Google Maps API to work but gives you much greater possibilities.

Simple Google Maps

  1. Installation

    It is recommended to use Composer while installing the module. Execute the command `composer require 'drupal/simple_gmap:^3.1'`.

    Map module installation

    Next, install the module from the Extend tab.

    How to install Simple Google Maps
  2. Configuration

    The extension does not require additional configuration. You can use it right away. To do that, go to Content types -> Manage fields -> Create a new field.

    New field

    Use the Plain text field.

    Address space

    Select the display format for the newly created field.

    Selection of display format

    In the Manage display tab, select Google Map from one-line address.

    Field configuration with address

    The initial configuration is ready. You can start filling in the field.

    Entering an address

    As you can see in the screenshot below, it works but the map format is only partially as expected.

    Customizing the map format

    To change it, return to the format configuration and press the gear on the right.

    Map format configuration

    The setting you are interested in is the width and height of the attached element.

    Map dimensions

    You can also set a default zoom, map type, and language.

    Map settings

    The map looks way better now.

    Setting up the map

Geofield Map

  1. Installation

    Similarly to the case above, execute the command `composer require 'drupal/geofield_map:^3.0'`.

    Geofield type

    In the Extend tab, find Geofield and Geofield Map and install the modules.

    Geofield Map - installation

    Geofield provides the field type used to display on the map by Geofield Map.

  2. Configuration

    You can find the module’s settings in Configuration -> System -> Geofield Map settings.

    System configuration

    To start using it, enter the API key obtained by signing into Google. If you don’t know how to get it, press the link next to the Get a key label.

    API key

    Now add the Geofield to the appropriate content type. We prepared an exemplary content type representing the company’s offices.

    Choosing a format

    In the Manage display section, select the Geofield Google Map format.

    Adding Geofield

    Fill in the created field with content. You should see a map.

    Map
  3. View

    Drupal Views is the easiest way to display all content types. Find the Structure -> Views -> Add view link in the admin menu.

    Drupal Views

    Enter the view name and select the content type.

    Contents

    To directly create a subpage with a map, select Create a page. You can also have the view generate a block that will be placed in a particular place on the website.

    Making a map

    After saving the configuration, you will be redirected to the view settings. Add a new field in the Fields group.

    View settings

    Select the Geofield field.

    Geofield module

    Select the data source that will be presented on the map: format -> settings -> data source.

    Title
    Styles - options

    If you did everything correctly, the map will now contain points representing the content of the selected content type.

    Dots

    In the display format settings, you can also set an individual marker (the field allows for using twig and substitution tokens). There are many more options here and depending on your needs, choose those that best suit your requirements.

    Style options

    We created a few more locations to illustrate how the view works.

    Example map view

    The function is now ready and fully operational.

Individual Map Styling

To make Google Maps for Drupal even more attractive for the end user, you can match the map’s display to your brand’s color scheme. Simply use the Google tool: https://mapstyle.withgoogle.com/.

Style options

If you are satisfied with the result, click finish and copy the code from the modal.

JSON code

Return to the view configuration. In the format -> settings tab, find the Custom Styled Map and paste the code in the options field. You should see the new, custom styling.

Ready-made map

Inserting a Google map in a Drupal website might attract users’ attention and show them how to reach your office.

There are many agencies offering Drupal maintenance services. If you have difficulties embedding a Google Map on a Drupal website, do not hesitate to get in touch!

Rate entry
5
Rate:  5 Amount of votes:  3

Thank you for rating the post!

We have more free content. Don't give them up!
Technology, SEO, marketing - a newsletter with tips you can implement right away! Straight to your inbox. Free and spam-free.
CAPTCHA