diff --git a/CHANGELOG.md b/CHANGELOG.md index b2c6542..91fbed9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ### Changed - Add support for Craft 5 +## 4.0.5 - 2024-04-04 +### Fixed +- Fix referred on Mapbox geo requests (Fixes #338, via @maxdmyers) +- Fix type errors in front-end usage (Fixes #379, via @samhibberd) +- Fix error when normalizing invalid location (Fixes #368, #380, via @Decyphr) +- Fix intermittent issues w/ Google Maps API loading (Fixes #294, via @davidwebca) +- Allow nullable zoom value (Fixes #381) +- Pass site language to embedded Google map (Fixes #373) +- Support casting map to string (Fixes #362) +- Remove reference to MaxMind Lite from docs (Fixes #358) + ## 4.0.4 - 2023-06-26 ### Fixed - Update settings autosuggest input (Fixes #374) diff --git a/docs/getting-started/config.md b/docs/getting-started/config.md index 7b7fd62..a9750b4 100644 --- a/docs/getting-started/config.md +++ b/docs/getting-started/config.md @@ -44,9 +44,6 @@ needed! ##### [ipstack](https://ipstack.com/product) ipstack offer free and paid-for versions of their API. -##### MaxMind Lite -No token required - ##### [MaxMind](https://www.maxmind.com/en/geoip2-precision-services) MaxMind offer free lookup database that must be stored locally, and a more accurate paid-for version of their API. @@ -197,7 +194,7 @@ constants. use ether\simplemap\services\GeoLocationService; return [ - 'geoLocationService' => GeoLocationService::MaxMindLite, + 'geoLocationService' => GeoLocationService::MaxMind, ]; ``` diff --git a/src/models/BaseLocation.php b/src/models/BaseLocation.php index 3a80a42..1513830 100644 --- a/src/models/BaseLocation.php +++ b/src/models/BaseLocation.php @@ -9,6 +9,7 @@ namespace ether\simplemap\models; use craft\helpers\Json; +use craft\helpers\Typecast; use Twig\Markup; use yii\base\Model; @@ -44,6 +45,8 @@ abstract class BaseLocation extends Model public function __construct ($config = []) { + Typecast::properties(static::class, $config); + parent::__construct($config); if ($this->address === null) @@ -112,4 +115,9 @@ public function address (array $exclude = [], string $glue = '
'): Markup return new Markup(implode($glue, $addr), 'utf8'); } + public function __toString(): string + { + return (string) $this->address([], ', '); + } + } diff --git a/src/models/Map.php b/src/models/Map.php index fcb62b0..3ee5294 100644 --- a/src/models/Map.php +++ b/src/models/Map.php @@ -40,7 +40,7 @@ class Map extends BaseLocation public ?int $fieldId = null; /** @var int */ - public int $zoom = 15; + public ?int $zoom = 15; /** @var int|float|null */ public int|null|float $distance = null; diff --git a/src/services/EmbedService.php b/src/services/EmbedService.php index 93e98cc..682fa71 100644 --- a/src/services/EmbedService.php +++ b/src/services/EmbedService.php @@ -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) { @@ -144,13 +145,9 @@ private function _embedGoogle (EmbedOptions $options, Settings $settings): strin $params = http_build_query([ 'key' => $settings->getMapToken(), 'callback' => $callbackName, + 'language' => Craft::$app->getSites()->getCurrentSite()->language, ]); - $this->_js( - 'https://maps.googleapis.com/maps/api/js?' . $params, - ['async' => '', 'defer' => ''] - ); - $js = <<id}; @@ -165,10 +162,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 '
'; diff --git a/src/services/GeoService.php b/src/services/GeoService.php index 898a5bf..c83ad64 100644 --- a/src/services/GeoService.php +++ b/src/services/GeoService.php @@ -680,6 +680,10 @@ public static function normalizeLocation (mixed $location, string $country = nul else if (!is_array($location) || !isset($location['lat'], $location['lng'])) $location = []; + if (!$location) { + $location = []; + } + return $location; } @@ -759,9 +763,13 @@ private static function _latLngFromAddress_Mapbox ($token, $address, $country): $url = str_replace('.json', rawurlencode(', ' . $country) . '.json', $url); } + $referer = Craft::$app->getRequest()->getIsConsoleRequest() + ? Craft::getAlias('@web') + : Craft::$app->urlManager->getHostInfo(); + $data = (string) static::_client()->get($url, [ 'headers' => [ - 'referer' => Craft::$app->urlManager->getHostInfo() + 'referer' => $referer, ] ])->getBody(); $data = Json::decodeIfJson($data);