Skip to content

Commit

Permalink
v3.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Nov 28, 2017
1 parent d35deac commit b1ae7ad
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
56 changes: 15 additions & 41 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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 (
Expand Down

0 comments on commit b1ae7ad

Please sign in to comment.