public function findDistance(Query $query, array $options) {
		$options += ['tableName' => null, 'sort' => true];
		$sql = $this->distanceExpr($options['lat'], $options['lng'], null, null, $options['tableName']);

		if ($query->autoFields() === null) {
			$query->autoFields(true);
		}

		$query->select(['distance' => $query->newExpr($sql)]);
		if (isset($options['distance'])) {
			// Some SQL versions cannot reuse the select() distance field, so we better reuse the $sql snippet
			$query->where(function ($exp) use ($sql, $options) {
				return $exp->lt($sql, $options['distance']);
			});
		}

		if ($options['sort']) {
			$sort = $options['sort'] === true ? 'ASC' : $options['sort'];
			$query->order(['distance' => $sort]);
		}

		return $query;
	}
public function findDistance(Query $query, array $options) {
		$options += ['tableName' => null, 'sort' => true];
		$sql = $this->distanceExpr($options['lat'], $options['lng'], null, null, $options['tableName']);

		if ($query->autoFields() === null) {
			$query->autoFields(true);
		}

		$query->select(['distance' => $query->newExpr($sql)]);
		if (isset($options['distance'])) {
			// Some SQL versions cannot reuse the select() distance field, so we better reuse the $sql snippet
			$query->where(function ($exp) use ($sql, $options) {
				return $exp->lt($sql, $options['distance']);
			});
		}

		if ($options['sort']) {
			$sort = $options['sort'] === true ? 'ASC' : $options['sort'];
			$query->order(['distance' => $sort]);
		}

		return $query;
	}
public function findDistance ( Query $ query , array $ options ) { $ options += [ 'tableName' => null , 'sort' => true ] ; $ sql = $ this -> distanceExpr ( $ options [ 'lat' ] , $ options [ 'lng' ] , null , null , $ options [ 'tableName' ] ) ; if ( $ query -> autoFields ( ) === null ) { $ query -> autoFields ( true ) ; } $ query -> select ( [ 'distance' => $ query -> newExpr ( $ sql ) ] ) ; if ( isset ( $ options [ 'distance' ] ) ) { // Some SQL versions cannot reuse the select() distance field, so we better reuse the $sql snippet $ query -> where ( function ( $ exp ) use ( $ sql , $ options ) { return $ exp -> lt ( $ sql , $ options [ 'distance' ] ) ; } ) ; } if ( $ options [ 'sort' ] ) { $ sort = $ options [ 'sort' ] === true ? 'ASC' : $ options [ 'sort' ] ; $ query -> order ( [ 'distance' => $ sort ] ) ; } return $ query ; }
Custom finder for distance.

Options:
- lat (required)
- lng (required)
- tableName
- distance
- sort

@param \Cake\ORM\Query $query Query.
@param array $options Array of options as described above
@return \Cake\ORM\Query
Custom finder for distance .