Filter locations by country in Smart Map I'm using 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.
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()-&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);
}


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