Skip to content

Commit

Permalink
Merge branch 'v4-dev' into v5-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Apr 4, 2024
2 parents 3922d11 + 5de1d9a commit 15a54ab
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions docs/getting-started/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -197,7 +194,7 @@ constants.
use ether\simplemap\services\GeoLocationService;

return [
'geoLocationService' => GeoLocationService::MaxMindLite,
'geoLocationService' => GeoLocationService::MaxMind,
];
```

Expand Down
8 changes: 8 additions & 0 deletions src/models/BaseLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace ether\simplemap\models;

use craft\helpers\Json;
use craft\helpers\Typecast;
use Twig\Markup;
use yii\base\Model;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -112,4 +115,9 @@ public function address (array $exclude = [], string $glue = '<br/>'): Markup
return new Markup(implode($glue, $addr), 'utf8');
}

public function __toString(): string
{
return (string) $this->address([], ', ');
}

}
2 changes: 1 addition & 1 deletion src/models/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 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 @@ -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 = <<<JS
let {$options->id};
Expand All @@ -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 '<div id="' . $options->id . '"></div>';
Expand Down
10 changes: 9 additions & 1 deletion src/services/GeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 15a54ab

Please sign in to comment.