`.
-| Option | Type | Default | Description
-|:-------|:------:|:-------:|-------------
-| `init` | _bool_ | `true` | Whether to automatically initialize the map via JavaScript.
+| Option | Type | Default | Description
+|:-----------|:--------:|:-------:|:------------
+| `api` | _object_ | `{}` | Optional parameters for the Google Maps API.
+| `assets` | _bool_ | `true` | Whether to preload the necessary JavaScript assets.
+| `init` | _bool_ | `true` | Whether to automatically initialize the map via JavaScript.
+| `callback` | _string_ | `null` | JavaScript function to run after the map has loaded.
By setting the `init` option to `false`, the map will not be automatically initialized in JavaScript. It must therefore be [manually initialized in JavaScript](/dynamic-maps/javascript-methods/#init-mapid-null-callback-null) when the page has completely rendered.
diff --git a/docs/updating-from-smart-map/customizing-the-map-in-twig.md b/docs/updating-from-smart-map/customizing-the-map-in-twig.md
index 3af64dc..5b3e6a4 100644
--- a/docs/updating-from-smart-map/customizing-the-map-in-twig.md
+++ b/docs/updating-from-smart-map/customizing-the-map-in-twig.md
@@ -20,10 +20,8 @@ Please take a closer look to see which items will need to be updated, added, or
| Option | | What Changed
|:---------------------|----|:-------------
-| `mapOptions` | ⭐ | **ADDED!** (new in the Google Maps plugin)
| `styles` | ⭐ | **ADDED!** (new in the Google Maps plugin)
-| `js` | ⭐ | **ADDED!** (new in the Google Maps plugin)
-| `callback` | ⭐ | **ADDED!** (new in the Google Maps plugin)
+| `mapOptions` | ⭐ | **ADDED!** (new in the Google Maps plugin)
| `maptype` | ❌ | REMOVED (configure via `mapOptions` instead)
| `scale` | ❌ | REMOVED (configure via `mapOptions` instead)
| `scrollwheel` | ❌ | REMOVED (configure via `mapOptions` instead)
diff --git a/src/models/DynamicMap.php b/src/models/DynamicMap.php
index 39e7a16..1123a43 100644
--- a/src/models/DynamicMap.php
+++ b/src/models/DynamicMap.php
@@ -22,6 +22,7 @@
use craft\web\View;
use doublesecretagency\googlemaps\fields\AddressField;
use doublesecretagency\googlemaps\GoogleMapsPlugin;
+use doublesecretagency\googlemaps\helpers\GoogleMaps;
use doublesecretagency\googlemaps\helpers\MapHelper;
use doublesecretagency\googlemaps\web\assets\JsApiAsset;
use Twig\Error\LoaderError;
@@ -84,19 +85,9 @@ public function __construct($locations = [], array $options = [], array $config
// Set internal map ID
$this->id = $options['id'];
- // Unless otherwise specified, preload the necessary JavaScript
- if (!isset($options['js']) || !is_bool($options['js'])) {
- $options['js'] = true;
- }
-
// Get view service
$view = Craft::$app->getView();
- // Load assets
- if ($options['js']) {
- $view->registerAssetBundle(JsApiAsset::class);
- }
-
// Whether devMode is enabled
$inDevMode = Craft::$app->getConfig()->general->devMode;
@@ -119,7 +110,7 @@ public function __construct($locations = [], array $options = [], array $config
];
// Prevent conflict between map ID and marker IDs
- unset($options['id'], $options['js']);
+ unset($options['id']);
// Create markers along with their corresponding info windows
$this->_initInfoWindows($locations, $options);
@@ -384,33 +375,48 @@ public function tag(array $options = []): Markup
throw new Exception('Model misconfigured. The map DNA is empty.');
}
- // Alias map from DNA
- $map =& $this->_dna[0];
-
- // If the first item is not a map, throw an error
- if ('map' != $map['type']) {
+ // If the first DNA item is not a map, throw an error
+ if ('map' != ($this->_dna[0]['type'] ?? false)) {
throw new Exception('Map model misconfigured. The chain must begin with a `map()` segment.');
}
- // Compile map container
- $html = Html::modifyTagAttributes('
Loading map...
', [
- 'id' => $this->id,
- 'class' => 'gm-map',
- 'data-dna' => Json::encode($this->_dna),
- ]);
+ // Get view service
+ $view = Craft::$app->getView();
+
+ // Unless otherwise specified, preload the necessary JavaScript assets
+ if (!isset($options['assets']) || !is_bool($options['assets'])) {
+ $options['assets'] = true;
+ }
+
+ // If no additional API parameters were specified, default to empty array
+ if (!isset($options['api']) || !is_array($options['api'])) {
+ $options['api'] = [];
+ }
+
+ // If we're permitted to load JS assets
+ if ($options['assets']) {
+ // Load assets with optional API parameters
+ GoogleMaps::loadAssets($options['api']);
+ }
// Initialize the map (unless intentionally suppressed)
if ($options['init'] ?? true) {
// Get optional callback
- $callback = ($map['options']['callback'] ?? 'null');
+ $callback = ($options['callback'] ?? 'null');
// Initialize Google Maps after page has loaded
$googleMapsInit = "googleMaps.init('{$this->id}', {$callback})";
$js = "addEventListener('load', function () {{$googleMapsInit}});";
// Register JS at the end of the page
- $view = Craft::$app->getView();
$view->registerJs($js, $view::POS_END);
}
+ // Compile map container
+ $html = Html::modifyTagAttributes('
Loading map...
', [
+ 'id' => $this->id,
+ 'class' => 'gm-map',
+ 'data-dna' => Json::encode($this->_dna),
+ ]);
+
// Return Markup
return Template::raw($html);
}