I've created this javascript (delivery.js) that is supposed to take a pre-existing div on a webform page and append a Google Map to it, along with a few circles of differing radius.
Here is my libraries.yml, I reference the GoogleMaps global in the script url to be used as the callback.
delivery:
version: 1.x
js:
js/delivery.js: {}
dependencies:
- core/jquery
- core/drupalSettings
maps:
js:
'https://maps.googleapis.com/maps/api/js?key=AIzaSyCP9_LHyWKv821WTcLWZd8GcLLHIHvPPU0&callback=GoogleMaps.initMap&v=weekly': { type: external, minified: true }
.module file attaching the libraries:
function order_form_webform_handler_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_submission_order_submission_node_5_add_form" || $form_id == "webform_submission_order_submission_test_form" ){
$form['#attached']['library'][] = 'order_form_webform_handler/delivery';
$form['#attached']['library'][] = 'order_form_webform_handler/maps';
}
}
Finally, my delivery.js file that does all the work:
var GoogleMaps = GoogleMaps || {};
(function ($, Drupal, GoogleMaps) {
Drupal.behaviors.attachMap = {
attach: function () {
GoogleMaps.initMap = function(){
// Create the map.
const map = new google.maps.Map(document.getElementById("edit-map"), {
zoom: 4,
center: { lat: 44.24, lng: -76.57 },
mapTypeId: "terrain",
});
let radii = [10000, 15000, 20000, 30000];
for(let i = 0; i < radii.length; i++){
const cityCircle = new google.maps.Circle({
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
center: { lat: 44.24, lng: -76.57 },
radius: radii[i],
});
}
}
}
};
})(jQuery, Drupal, GoogleMaps);
However, when I try to run this, I get this error:
.te {message: 'GoogleMaps.initMap is not a function', stack: 'Error\n at _.te.captureStackTrace (https://maps.PPU0&callback=GoogleMaps.initMap&v=weekly:212:276', name: 'InvalidValueError'}
Is there a better method of invoking the callback that I don't know about?