Mapping

by Gisle Hannemyr

This case study describes how to work with mapping extensions to Drupal 9.

Table of contents

Drupal extensions discussed in this chapter: Address, Geocoder, Geofield, Leaflet.

Introduction

Geofield

Geofield is an advanced module for storing, managing and representing dynamic Geographic data.

$ composer require 'drupal/geofield:^1.56'

Create a content type named "SNP Place" with fields:

EntityTypeManagerInterface $entity_type_manager) $this->entityTypeManager = $entity_type_manager; EntityTypeManagerInterface $entity_type_manager $this->entityTypeManager = $entity_type_manager;

Leaflet

The Drupal Leaflet bridge module provides integration with the Leaflet JS mapping library. It requires Geofield to create a "Location" content type to hold the coordinates for the location.

$ composer require 'drupal/leaflet:^10.2'

Video tutorials:

Geocoder

[Not used by SNP.]

Geocoding is converting text into geographic coordinates (forward geocoding), and vice versa (reverse geocoding). For example, turning "2 Lincoln Memorial Circle NW" into "-77.050,38.889" is forward geocoding. Reverse geocoding converts geographic coordinates into a text description, for example, turning "-77.050,38.889" into "2 Lincoln Memorial Circle NW".

The Drupal Geocoder bridge module provides a Drupal wrapper and interface for the Geocoder PHP library. This is library which helps you build geo-aware applications by providing a powerful abstraction layer for geocoding manipulations.

The version 3/4 of the Drupal 10 bridge module provides an interface to Geocoder PHP library - version 4 (GitHub).

Installing and enabling Geocoder

First, download the module running the following shell command from you siteroot:

$ composer require 'drupal/geocoder:^3.35'
$ composer require 'drupal/geocoder:^4.10'

This will also download the Geocoder PHP library (willdurand/geocoder) as a dependency.

The project contains the following modules:

Use drush to enable the module:

$ drush en geocoder

You may later enable the three submodules. Before you can enable two of them you first need to download their dependencies:

$ composer require 'drupal/address:^1.12'
$ composer require 'drupal/geofield:^1.56'

To enable the the three sub-modules:

$ drush en geocoder_field
$ drush en geocoder_address
$ drush en geocoder_geofield

Configuring Geocoder

To use the module, you need to configure a provider. All existing providers appear @ packagist.org: provider packages.

Choose the Geocoder Provider you want to use and also add it as a required dependency to your project. (you will find that as a possible geocoder provider choice in the "add provider" options of the Geocoder module settings page ('/admin/config/system/geocoder')

For example if you want to use GraphHopper or Google Maps as your provider, select one the following:

$ composer require geocoder-php/graphhopper-provider
$ composer require geocoder-php/google-maps-provider

Add the API key.

Using Geocoder

[TBA]

Final word

[TBA]


Last update: 2021-02-03 [gh].