Loading Google JS when only opting for Leaflet

Created on 19 February 2023, over 1 year ago
Updated 30 August 2023, about 1 year ago

Sure I've seen stuff about this before in the issue queue, but can't quite find anything exactly like this.
Spotted while I was prodding the javascript to include the refresh and paying attention to which versions of the JS were being loaded.

Problem/Motivation

Using Geofield Map - without using bundled geocoding - and want to just use locally hosted leaflet JS.
[aside: we're doing the geocoding using Geocoder module integration but then with ajax from an address field autocomplete https://git.drupalcode.org/project/geo_entity/-/blob/1.0.x/modules/geo_e... ]

Steps to reproduce

Configure Geofield Map widget, but not Geocoding.

Gmap Api Key missing - Set it in the Geofield Map Configuration Page.
Google Maps rendering and Geocode & ReverseGeocode functionalities (provided by the Google Maps Geocoder) not available.

Search Address Functionalities based on:
- Geofield Map GMaps API Key Geocoder Not available
- Google Places Autocomplete Service: disabled

Map Library: Leaflet Js
Map Type: OpenStreetMap_Mapnik
Map Type Selector: enabled
Map Dimensions - Width: 100%; Height: 450px;
Zoom Levels - Start: 6; Focus: 12; Min: 0; Max: 22;
HTML5 Geolocation button: disabled
Click to find marker: disabled
Click to place marker: disabled
Lat/Lon coordinates hidden: disabled
Geoaddress Field: - any -

This line:
https://git.drupalcode.org/project/geofield_map/-/blob/3.0.x/js/geofield...

          // Google Maps library shouldn't be requested if the following
          // conditions apply:
          // - leaflet js is the chosen map library;
          // - geocoder integration is enabled;
          if (options.map_library === 'leaflet' && options.gmap_geocoder) {
            // Check and wai for the Leaflet module to be loaded.
            let checkLeafletExist = setInterval(function() {
               if (window.hasOwnProperty('L')) {
                  Drupal.geoFieldMap.map_initialize(options, context);
                  clearInterval(checkLeafletExist);
               }
            }, 100);
          }
          else {
            // Load before the Gmap Library, if needed, then initialize the Map.
            Drupal.geoFieldMap.loadGoogle(mapid, options.gmap_api_key, function () {
              Drupal.geoFieldMap.map_initialize(options, context);
            });
          }
        }

mean that the Google Maps are loaded.

Proposed resolution

It is almost as simple as changing the logic, of that if statement. But further on it still makes assumptions and JS throws map already defined errors. So it needs a bit more work than that.

I also want to check that I don't mis-understand the _intention_ of that if statement.

🐛 Bug report
Status

Closed: outdated

Version

3.0

Component

Code

Created by

🇳🇱Netherlands ekes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024