diff --git a/CHANGELOG.md b/CHANGELOG.md index 940a176..a133deb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 3.0.4 - 2017-11-28 +### Added +- Added ability to restrict location search by country + +### Changed +- New icon! + ## 3.0.3 - 2017-11-08 ### Added - It's now possible to save the map field with only an address! Useful for populating the field from the front-end. (Requires the Geocoding API). diff --git a/README.md b/README.md index 258258d..37638a7 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,17 @@ You can search for elements using the location specified in your map field. When ```twig {% set entries = craft.entries.myMapField({ - location: 'Maidstone, Kent, UK', + location: 'Maidstone, Kent', + country: 'GB', radius: 100, unit: 'mi' }).orderBy('distance').all() %} ``` - `location`: Can either be an address string (requires a Google Maps Geocoding API key) or a Lat Lng Array (`{ 'lat': 51.27219908, 'lng': 0.51545620 }`). -- `radius`: The radius around the location to search. Defaults to `50`. -- `unit`: The unit of measurement for the search. Can be either `km` (kilometers) or `mi` (miles). Defaults to `km`. +- `country`: *Optional*. Restrict the search to a specific country (useful for non-specific searches, i.e. town name). Must be valid [2-letter ISO code](https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes) (recommended), or full country name. +- `radius`: *Optional*. The radius around the location to search. Defaults to `50`. +- `unit`: *Optional*. The unit of measurement for the search. Can be either `km` (kilometers) or `mi` (miles). Defaults to `km`. ### API Keys diff --git a/composer.json b/composer.json index b91ab31..538e9bb 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ether/simplemap", "description": "A beautifully simple Google Map field type.", - "version": "3.0.3", + "version": "3.0.4", "type": "craft-plugin", "license": "MIT", "minimum-stability": "dev", diff --git a/src/icon.svg b/src/icon.svg index ef756fd..56a2748 100644 --- a/src/icon.svg +++ b/src/icon.svg @@ -1,41 +1,15 @@ - - - - -icon -Created with Sketch. - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + icon + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/src/services/MapService.php b/src/services/MapService.php index f3defd6..b49cbbb 100644 --- a/src/services/MapService.php +++ b/src/services/MapService.php @@ -46,22 +46,26 @@ class MapService extends Component * Converts the given address to Lat/Lng * * @param string $address + * @param string|null $country * * @return array|null */ - public static function getLatLngFromAddress ($address) + public static function getLatLngFromAddress ($address, $country = null) { if (array_key_exists($address, self::$_cachedAddressToLatLngs)) { return self::$_cachedAddressToLatLngs[$address]; } - $browserApiKey = self::_getAPIKey(); + $apiKey = self::_getAPIKey(); - if (!$browserApiKey) return null; + if (!$apiKey) return null; $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address) - . '&key=' . $browserApiKey; + . '&key=' . $apiKey; + + if ($country) + $url .= '&components=country:' . rawurldecode($country); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); @@ -421,6 +425,9 @@ private function _calculateDistance (Map $map) private function _searchLocation (ElementQuery $query, $value) { $location = $value['location']; + $country = array_key_exists('country', $value) + ? $value['country'] + : null; $radius = array_key_exists('radius', $value) ? $value['radius'] : 50.0; @@ -436,7 +443,7 @@ private function _searchLocation (ElementQuery $query, $value) else if (!in_array($unit, ['km', 'mi'])) $unit = 'km'; if (is_string($location)) - $location = self::getLatLngFromAddress($location); + $location = self::getLatLngFromAddress($location, $country); if (is_array($location)) { if (