我正在尝试计算两个地理位置之间的距离。用户输入一个位置。
用户输入的位置按型号计算并存储在变量中。
$ulatitude=$userLocation['Location']['latitude'];
$ulongitude=$userLocation['Location']['longitude'];
我使用下面的代码来计算距离。
$this->User->virtualFields['lowestDistance'] = 'round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) ';
$order = "User.lowestDistance";
$options = array(
'fields' => '
User.*, Profile.*,
round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) AS lowestDistance,
',
'order' => array($order =>'asc'),
);
上面的代码抛出错误:未知列$ ulatitude。
还有其他方法吗?
答案 0 :(得分:1)
考虑:
<?php
$foo = "A VARIABLE";
echo 'foo is $foo' . "\n";
echo "foo is $foo" . "\n";
这将输出:
-> php example.php
foo is $foo
foo is A VARIABLE
在问题中,所有内容都是单引号,它们是文字字符串。
另请注意:
'fields' => 'User.*, Profile.*, ...
通常是一个数组,当有多个值时更有意义:
'fields' => [
'User.*',
'Profile.*',
...
]
但是,既然你要声明所有字段加上一个虚拟字段,那么无论如何它都会包含在查找调用中 - 只需从查找参数中删除字段:
$this->User->virtualFields['lowestDistance'] = "round(...";
$options = [
//'fields' => [], Not necessary
'order' => [$order =>'asc']
];
$withLowestDistance = $this->User->find('all', $options);