I'm using the Element API plugin to try and search for addresses using minimum and maximum lat & lng values and then using target element within the criteria parameters to filter the results however it doesn't seem to be working based on the code below.

$locationCriteria = craft()->elements->getCriteria('Entry');    
$locationCriteria->lat = array('and', '>='.$max_min_values['min_latitude'], '<='.$max_min_values['max_latitude']);
$locationCriteria->long =  array('and', '>='.$max_min_values['min_longitude'], '<='.$max_min_values['max_longitude']);
$locations = $locationCriteria->find();

return [
            'criteria' => [
                'section' => 'xxx',
                'relatedTo' => [
                    ['targetElement' => $locations],
            'elementType' => ElementType::Entry,
            'transformer' => new Search(),

Does anyone have any ideas?

  • 2
    Not a particular answer to your question because I never used the element api - just a general thing - but array('and', '>='.$max_min_values['min_latitude'], '>='.$max_min_values['max_latitude']); will get you only records with a latitude >= max latitude. The second thing: it's better to use $locationCriteria->ids() since you don't need to populate the models. Performance wise using find in that manner is wasted Commented Nov 28, 2017 at 5:39


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.