<p>Just figured out how to actually use the Smart Map plugin with the Element API plugin:</p>

<pre><code>...
'endpoints' =&gt; [
    'entries.json' =&gt; [
        'criteria' =&gt; [
            'section' =&gt; 'sectionHandle',
            'address' =&gt; array(
                'target' =&gt; '50.25641910,6.10379780',
                'range' =&gt; 20
            )
        ],
    ]
]
...
</code></pre>

CC BY-SA 3.0
2016-05-20T09:36:02.520Z
2016-05-20T09:36:02.520Z
medoingthings
<p>You're very close!</p>
<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;myFieldHandle = array(
    'target' =&gt; 90210,
    'range' =&gt; 100
);
$criteria-&gt;order = 'distance';

$results = $criteria-&gt;find();
</code></pre>
<p>I've never actually tested it with the Element API plugin, but in theory it should work just fine.</p>
<p>In response to this question, I've added <a href="https://plugins.doublesecretagency.com/smart-map/sorting-entries-by-closest-locations/#php-in-a-separate-plugin-or-module" rel="nofollow noreferrer">instructions for PHP</a> to the docs.</p>

CC BY-SA 4.0
2016-02-13T06:54:15.567Z
2021-01-01T06:34:27.707Z
2021-01-01T06:34:27.707Z
Lindsey D
<p>I want to sort entries with a Smart Map address field for closest location. I know how it should be done in a template: </p>

<pre><code>{% set entries = craft.entries.myAddress(params).order('distance').find() %}
</code></pre>

<p>Only, I would like to do so in my business logic and not in my template. I tried something like this.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'houses';
$criteria-&gt;myAddress-&gt;target = '90210';
$criteria-&gt;order = 'myAddress.distance';
</code></pre>

<p>However, this does not work. </p>

<p>Is this possible? I tried related options but can't seem to get anything to work. Can I also do this with the Element API plugin to return the results as JSON?</p>

<elementcriteriamodel><plugin-elementapi><plugin-smartmap>
Smart Map - Sorting for closest location in PHP
# Smart Map - Sorting for closest location in PHP

(Asked by: username_0 on 2016-02-10)

I want to sort entries with a Smart Map address field for closest location. I know how it should be done in a template: 
{% set entries = craft.entries.myAddress(params).order('distance').find() %}

Only, I would like to do so in my business logic and not in my template. I tried something like this.
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'houses';
$criteria->myAddress->target = '90210';
$criteria->order = 'myAddress.distance';

However, this does not work. 
Is this possible? I tried related options but can't seem to get anything to work. Can I also do this with the Element API plugin to return the results as JSON?

**Tags:** elementcriteriamodel, plugin-elementapi, plugin-smartmap


---

## 2 answers

### Answer by username_2 on 2016-02-13. Score: 4 | Accepted answer

You're very close!
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->myFieldHandle = array(
    'target' => 90210,
    'range' => 100
);
$criteria->order = 'distance';

$results = $criteria->find();

I've never actually tested it with the Element API plugin, but in theory it should work just fine.
In response to this question, I've added instructions for PHP to the docs.


---

### Answer by username_1 on 2016-05-20. Score: 1

Just figured out how to actually use the Smart Map plugin with the Element API plugin:
...
'endpoints' => [
    'entries.json' => [
        'criteria' => [
            'section' => 'sectionHandle',
            'address' => array(
                'target' => '50.25641910,6.10379780',
                'range' => 20
            )
        ],
    ]
]
...


---

