Smart Map - Sorting for closest location in PHP 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()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'houses';
$criteria-&gt;myAddress-&gt;target = '90210';
$criteria-&gt;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?
You're very close!
$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();

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.