Answers.Body: <p>What I've done to do something very similar is to add a template variable to my BusinessLogic plugin, that takes the ElementCriteriaModel, prepared with all other parameters e.g. section, limit and SmartMap set. The plugin then translates the model into a <a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand" rel="nofollow noreferrer">Yii CDbCommand</a> using <a href="http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail" rel="nofollow noreferrer"><code>buildElementsQuery</code></a>, adds the additional conditions to the query and returns the further filtered entry models to the template.</p>
  
  <p>Here's my service function with modifications for your country filtering needs. It#s untested but should probably work as is:</p>
  
  <pre><code>public function addCountryCriteria(ElementCriteriaModel $criteria, $country)
  {
      $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  
      // Add where condition for country filter
      $params = array(':country' =&gt; $country);
      $conditions = array('smartmap_addresses.country = :country');
  
      $query-&gt;andWhere($conditions, $params);
  
      // Join addresses table to bring in the state
      $query-&gt;leftJoin('smartmap_addresses smartmap_addresses', 'smartmap_addresses.elementId = entries.id');
  
      // Get query results and populate entry models
      $results = $query-&gt;queryAll();
  
      return EntryModel::populateModels($results);
  }
  </code></pre>
  
  <p>Also have a look at my other answers on how to add conditions to CDbCommand from the template:</p>
  
  <ul>
  <li><a href="https://craftcms.stackexchange.com/q/10149/125">How do I select entries that have a particular block type?</a></li>
  <li><a href="https://craftcms.stackexchange.com/q/9999/125">Filtering Matrix through field value and multiple relations</a></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-06T16:53:05.967Z
Answers.Text: Great answer! For folks comfortable with plugin dev, this would be a slick way to pull it off in the initial query. My answer caters more towards folks who'd rather just filter it in Twig. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-06T16:56:41.710Z
Answers.Text: Yes exactly my thoughts, now there's two good answers! :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T11:36:57.463Z
Answers.Text: I can only select one correct answer to this. I haven't tried your way, but it looks very good! Thanks!
Answers.UserDisplayName: Tom De Smet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T11:43:36.113Z
Answers.Text: @TomDeSmet try it, it shouldn't take longer than 5mins. to set this up. Do you need pagination – works with this approach. ;)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T11:45:45.007Z
Answers.Text: Colleague of mine is allready putting the site live and I'm allready working on another project :) Deadlines, deadlines! ;)
Answers.UserDisplayName: Tom De Smet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-28T08:28:41.750Z
Answers.Text: wow that is pretty awsm. i might be able to use this approach in a plugin i'm working on right now. thanks!
Answers.UserDisplayName: bhu Boue vidya
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-28T11:59:44.653Z
Answers.Text: @bhuBouevidya you should also have a look at this follow-up: [Modify ElementCriteria and use it for paginate](http://craftcms.stackexchange.com/q/12189/125)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-28T15:36:00.453Z
Answers.Text: @carlcs thx for that tip! that was a great answer to that question. cheers...
Answers.UserDisplayName: bhu Boue vidya
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-06T16:45:53.023Z
Answers.LastActivityDate: 2015-08-06T16:45:53.023Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>At the moment, there's no in-plugin way to handle this. However, you can probably do it out-of-plugin, once you've retrieved a larger set of results.</p>
  <p>So first, you'd <a href="https://plugins.doublesecretagency.com/smart-map/sorting-entries-by-closest-locations/" rel="nofollow noreferrer">get the largest possible results set:</a></p>
  <pre><code>{% set params = {
      target: targetLocation,
      range:  10000,
      units:  'kilometers'
  } %}
  
  {% set entries = craft.entries.myFieldHandle(params).order('distance').find() %}
  </code></pre>
  <p>Then, you'd cycle through those results to filter only the entries from your desired country:</p>
  <pre><code>{% set desiredCountry = 'France' %}
  {% set filteredByCountry = [] %}
  
  {% for entry in entries %}
      {% if entry.myFieldHandle.country == desiredCountry %}
          {% set filteredByCountry = filteredByCountry|merge([entry]) %}
      {% endif %}
  {% endfor %}
  </code></pre>
  <p>Once you've done that, you'll have a <code>filteredByCountry</code> array of entries, which should be exactly the results set you were looking for.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T11:35:57.427Z
Answers.Text: I did it the exact same way before I read your post :) Thanks anyway for the quick response! Love the power of Twig!
Answers.UserDisplayName: Tom De Smet
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-08-06T16:48:15.370Z
Answers.LastActivityDate: 2021-01-01T06:21:38.443Z
Answers.LastEditDate: 2021-01-01T06:21:38.443Z
Answers.UserDisplayName: Lindsey D
Body: <p>I'm using <a href="https://plugins.doublesecretagency.com/smart-map/" rel="nofollow noreferrer">Smart Map</a> to find locations ordered by distance. But I can only set 3 parameters: target, range and units. However, when my range goes beyond the country borders I obviously get results in other countries. I want to list all locations within a country by distance, but limited to that country.</p>
  <p>Is it possible to do this? I tried related items, but can't seem to get it to work. And I don't see any option in Smart Map either.</p>
  
Tags: <relations><plugin-smartmap>
Title: Filter locations by country in Smart Map
ThreadText: # Filter locations by country in Smart Map
  
  (Asked by: username_0 on 2015-08-06)
  
  I'm using [Smart Map](https://plugins.doublesecretagency.com/smart-map/) to find locations ordered by distance. But I can only set 3 parameters: target, range and units. However, when my range goes beyond the country borders I obviously get results in other countries. I want to list all locations within a country by distance, but limited to that country.
  
  Is it possible to do this? I tried related items, but can't seem to get it to work. And I don't see any option in Smart Map either.
  
  **Tags:** relations, plugin-smartmap
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-08-06. Score: 4 | Accepted answer
  
  At the moment, there's no in-plugin way to handle this. However, you can probably do it out-of-plugin, once you've retrieved a larger set of results.
  
  So first, you'd [get the largest possible results set:](https://plugins.doublesecretagency.com/smart-map/sorting-entries-by-closest-locations/)
  
  ```
  {% set params = {
      target: targetLocation,
      range:  10000,
      units:  'kilometers'
  } %}
  
  {% set entries = craft.entries.myFieldHandle(params).order('distance').find() %}
  
  ```
  
  Then, you'd cycle through those results to filter only the entries from your desired country:
  
  ```
  {% set desiredCountry = 'France' %}
  {% set filteredByCountry = [] %}
  
  {% for entry in entries %}
      {% if entry.myFieldHandle.country == desiredCountry %}
          {% set filteredByCountry = filteredByCountry|merge([entry]) %}
      {% endif %}
  {% endfor %}
  
  ```
  
  Once you've done that, you'll have a `filteredByCountry` array of entries, which should be exactly the results set you were looking for.
  
  > **Comments:**
  > **username_0**: I did it the exact same way before I read your post :) Thanks anyway for the quick response! Love the power of Twig!  
  
  ---
  
  ### Answer by username_1 on 2015-08-06. Score: 5
  
  What I've done to do something very similar is to add a template variable to my BusinessLogic plugin, that takes the ElementCriteriaModel, prepared with all other parameters e.g. section, limit and SmartMap set. The plugin then translates the model into a [Yii CDbCommand](http://www.yiiframework.com/doc/api/1.1/CDbCommand) using [`buildElementsQuery`](http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail), adds the additional conditions to the query and returns the further filtered entry models to the template.
  
  Here's my service function with modifications for your country filtering needs. It#s untested but should probably work as is:
  
  ```
  public function addCountryCriteria(ElementCriteriaModel $criteria, $country)
  {
      $query = craft()->elements->buildElementsQuery($criteria);
  
      // Add where condition for country filter
      $params = array(':country' => $country);
      $conditions = array('smartmap_addresses.country = :country');
  
      $query->andWhere($conditions, $params);
  
      // Join addresses table to bring in the state
      $query->leftJoin('smartmap_addresses smartmap_addresses', 'smartmap_addresses.elementId = entries.id');
  
      // Get query results and populate entry models
      $results = $query->queryAll();
  
      return EntryModel::populateModels($results);
  }
  
  ```
  
  Also have a look at my other answers on how to add conditions to CDbCommand from the template:
  
  * [How do I select entries that have a particular block type?](https://craftcms.stackexchange.com/q/10149/125)
  * [Filtering Matrix through field value and multiple relations](https://craftcms.stackexchange.com/q/9999/125)
  
  > **Comments:**
  > **username_2**: Great answer! For folks comfortable with plugin dev, this would be a slick way to pull it off in the initial query. My answer caters more towards folks who'd rather just filter it in Twig. :)  
  > **username_1**: Yes exactly my thoughts, now there's two good answers! :)  
  > **username_0**: I can only select one correct answer to this. I haven't tried your way, but it looks very good! Thanks!  
  > **username_1**: @username_0 try it, it shouldn't take longer than 5mins. to set this up. Do you need pagination – works with this approach. ;)  
  > **username_0**: Colleague of mine is allready putting the site live and I'm allready working on another project :) Deadlines, deadlines! ;)  
  > **username_3**: wow that is pretty awsm. i might be able to use this approach in a plugin i'm working on right now. thanks!  
  > **username_1**: @username_3 you should also have a look at this follow-up: [Modify ElementCriteria and use it for paginate](http://craftcms.stackexchange.com/q/12189/125)  
  > **username_3**: @username_1 thx for that tip! that was a great answer to that question. cheers...  
  
  ---
  
  