<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>

CC BY-SA 3.0
2015-08-06T16:53:05.967Z
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. :)
Lindsey D
CC BY-SA 3.0
2015-08-06T16:56:41.710Z
Yes exactly my thoughts, now there's two good answers! :)
carlcs
CC BY-SA 3.0
2015-08-12T11:36:57.463Z
I can only select one correct answer to this. I haven't tried your way, but it looks very good! Thanks!
Tom De Smet
CC BY-SA 3.0
2015-08-12T11:43:36.113Z
@TomDeSmet try it, it shouldn't take longer than 5mins. to set this up. Do you need pagination – works with this approach. ;)
carlcs
CC BY-SA 3.0
2015-08-12T11:45:45.007Z
Colleague of mine is allready putting the site live and I'm allready working on another project :) Deadlines, deadlines! ;)
Tom De Smet
CC BY-SA 3.0
2015-11-28T08:28:41.750Z
wow that is pretty awsm. i might be able to use this approach in a plugin i'm working on right now. thanks!
bhu Boue vidya
CC BY-SA 3.0
2015-11-28T11:59:44.653Z
@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)
carlcs
CC BY-SA 3.0
2015-11-28T15:36:00.453Z
@carlcs thx for that tip! that was a great answer to that question. cheers...
bhu Boue vidya
CC BY-SA 3.0
2015-08-06T16:45:53.023Z
2015-08-06T16:45:53.023Z
2017-04-13T12:51:00.160Z
carlcs
<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>

CC BY-SA 3.0
2015-08-12T11:35:57.427Z
I did it the exact same way before I read your post :) Thanks anyway for the quick response! Love the power of Twig!
Tom De Smet
CC BY-SA 4.0
2015-08-06T16:48:15.370Z
2021-01-01T06:21:38.443Z
2021-01-01T06:21:38.443Z
Lindsey D
<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>

<relations><plugin-smartmap>
Filter locations by country in Smart Map
# 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:
{% 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 using buildElementsQuery, 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?
Filtering Matrix through field value and multiple relations

> **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**: @TomDeSmet 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**: @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)  
> **username_3**: @username_1 thx for that tip! that was a great answer to that question. cheers...  

---

