Skip to content

Commit

Permalink
Merge pull request #371 from davidwebca/v4
Browse files Browse the repository at this point in the history
Fix Google Maps api load order + provide an extra callback for devs to hook into
  • Loading branch information
Tam authored Apr 4, 2024
2 parents 4fe3fd0 + 3720cd3 commit 73fb1d3
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/services/EmbedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private function _embedGoogle (EmbedOptions $options, Settings $settings): strin
{
$view = Craft::$app->getView();
$callbackName = 'init_' . $options->id;
$loadedCallbackName = $options->id . '_loaded';

$mapTypeId = match ($settings->mapTiles)
{
Expand Down Expand Up @@ -146,11 +147,6 @@ private function _embedGoogle (EmbedOptions $options, Settings $settings): strin
'callback' => $callbackName,
]);

$this->_js(
'https://maps.googleapis.com/maps/api/js?' . $params,
['async' => '', 'defer' => '']
);

$js = <<<JS
let {$options->id};
Expand All @@ -165,10 +161,15 @@ function {$callbackName} () {
}
JS;

$css = $this->_getCss($options);

$view->registerScript($js, View::POS_END);

$view->registerJs($js, View::POS_END);
$this->_js(
'https://maps.googleapis.com/maps/api/js?' . $params,
['async' => '', 'defer' => '', 'onload' => "typeof {$loadedCallbackName} != 'undefined' && {$loadedCallbackName}()"]
);

$css = $this->_getCss($options);
$css && $view->registerCss($css);

return '<div id="' . $options->id . '"></div>';
Expand Down

0 comments on commit 73fb1d3

Please sign in to comment.