Skip to content

Commit

Permalink
Merge branch 'v2-dev' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Nov 28, 2017
2 parents 0200b44 + 4423ec8 commit e7ec670
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 49 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,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'
}).order('distance') %}
}).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 }` or `craft.simpleMap.latLng(51.27219908, 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`.
- `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 }`).
- `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 Expand Up @@ -95,6 +97,10 @@ var map = new mapboxgl.Map({

## Changelog

### 1.7.2
- Added ability to restrict location search by country
- New icon!

### 1.7.1
- It is now possible to save the field using only an address (useful for saving via the front-end, requires Geocoding).
- Improved the field's validation.
Expand Down
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,14 @@
"[Added] It is now possible to save the field using only an address (useful for saving via the front-end, requires Geocoding)",
"[Improved] Improved the field's validation"
]
},
{
"version": "1.7.2",
"downloadUrl": "https://github.com/ethercreative/simplemap/archive/v1.7.2.zip",
"date": "2017-11-28T14:10:00-08:00",
"notes": [
"[Added] Added ability to restrict location search by country",
"[Changed] New icon!"
]
}
]
2 changes: 1 addition & 1 deletion simplemap/SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getDescription()

public function getVersion()
{
return '1.7.1';
return '1.7.2';
}

public function getSchemaVersion()
Expand Down
56 changes: 15 additions & 41 deletions simplemap/resources/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions simplemap/services/SimpleMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function modifyQuery (DbCommand &$query, $params = array())
private function _searchLocation (DbCommand &$query, $params)
{
$location = $params['location'];
$country = array_key_exists('country', $params) ? $params['country'] : null;
$radius = array_key_exists('radius', $params) ? $params['radius'] : 50.0;
$unit = array_key_exists('unit', $params) ? $params['unit'] : 'kilometers';

Expand All @@ -198,7 +199,7 @@ private function _searchLocation (DbCommand &$query, $params)
if (!in_array($unit, array('km', 'mi'))) $unit = 'km';

if (is_string($location))
$location = self::getLatLngFromAddress($location);
$location = self::getLatLngFromAddress($location, $country);
if (is_array($location)) {
if (!array_key_exists('lat', $location) ||
!array_key_exists('lng', $location))
Expand Down Expand Up @@ -259,11 +260,13 @@ private function _searchLocation (DbCommand &$query, $params)
* Find lat/lng from string address
*
* @param $address
* @param string|null $country
*
* @return null|array
*
* TODO: Cache results?
*/
public static function getLatLngFromAddress ($address)
public static function getLatLngFromAddress ($address, $country = null)
{
$browserApiKey = self::getAPIKey();

Expand All @@ -273,6 +276,9 @@ public static function getLatLngFromAddress ($address)
. rawurlencode($address)
. '&key=' . $browserApiKey;

if ($country)
$url .= '&components=country:' . rawurldecode($country);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down

0 comments on commit e7ec670

Please sign in to comment.