Ok I got it with a little work around.
I use hook_views_query_alter() to get all the distances of the relevant nodes. So I build an own query like this:
function mymodule_views_query_alter(&$view, &$query) {
if($view->name === 'my_view' && $view->current_display === 'my_display') {
// build the query to get the distances
$location_query = db_select($query->table_queue['node']['table'], $query->table_queue['node']['alias']);
$location_query->leftJoin('location_instance', 'li', $query->table_queue['node']['alias'] . '.vid = li.vid');
$location_query->leftJoin('location', 'l', 'li.lid = l.lid');
$location_query->leftJoin('field_data_field_distance', 'd', $query->table_queue['node']['alias'] . '.nid = d.entity_id');
$location_query->fields('l', array('latitude', 'longitude'))
->fields('d', array('field_distance_value'))
->fields($query->table_queue['node']['alias'], array('nid'));
// consider all filters which were set in the views ui
foreach ($query->where[1]['conditions'] as $condition) {
$location_query->condition($condition['field'], $condition['value'], $condition['operator']);
}
// execute the query
$result = $location_query->execute()->fetchAll();
// go through all results to check the distance
foreach($result as $node) {
// do the calculation of the distance
// ...
// check if distance is close enough
if($distance <= $node->field_distance_value) {
$nids[] = $node->nid;
}
}
// remove all conditions from the views query, because we already used them
unset($query->where[1]['conditions']);
// let views just load all the relevant NIDs
$query->where[1]['conditions'][0] = array(
'field' => $query->table_queue['node']['alias'] . '.nid',
'value' => $nids,
'operator' => 'in',
);
}
}
Well, in this way I anticipate the Views query and change it, so that Views just needs to load the NIDs which were required.