Codeigniter和SQL / Haversine公式 [英] Codeigniter and SQL/Haversine Formula
问题描述
我正在为项目使用Codeigniter,并且在数据库上有一些带有经度和纬度输入的位置。我正在尝试在一定距离(50公里)内获取积分,并尝试使用Haversine公式。但是,在我的控制器上查看并不会打印出结果。我需要知道的是,是否需要进行任何调整才能使模型功能正常工作。
I am using Codeigniter for a project and have some locations with latitude and longitude inputs on the database. I'm trying to get points within a certain distance (50km), and attempting it with the Haversine Formula. However, on my controller and view it doesn't print out the results. What I need to know is if there's any adjustments I need to make to get the model function working.
注意:数据库表具有列名称location_latitude和location_longitude。
Note: The DB table has the column names location_latitude and location_longitude.
//base function to compare distance with
$setlat = 13.5234412;
$setlong = 144.8320897;
//query with Haversin formula
$awaka = "SELECT 'location_id',
( 3959 * acos( cos( radians(?) ) * cos( radians('location_latitude') ) * cos( radians('location_longitude') - radians(?) )
+ sin( radians(?) ) * sin( radians('location_latitude') ) ) ) AS 'distance'
FROM 'locations' HAVING 'distance < 5'";
$result = $this->db->query($awaka, array($setlat, $setlong, $setlat));
echo $result;
推荐答案
似乎您被Haversine绊倒了式。您在第三个余弦
$ setlong 和 location_longitude
项>学期。我将它们切换到了这个位置:
It appears you got a little tripped up by the Haversine formula. You switched the $setlong
and location_longitude
terms in the third cosine
term. I switched them around to get this:
$setlat = 13.5234412;
$setlong = 144.8320897;
$awaka = "SELECT 'location_id',
( 3959 * acos( cos( radians(?) ) * cos( radians('location_latitude') ) * cos( radians(?) - radians('location_longitude') )
+ sin( radians(?) ) * sin( radians('location_latitude') ) ) ) AS 'distance'
FROM 'locations' HAVING 'distance < 5'";
$result = $this->db->query($awaka, array($setlat, $setlong, $setlat));
echo $result;
看看此页面,该页面显示您必须编写Haversine公式才能找到两点之间的距离。
Have a look at this page, which shows you have to code up the Haversine formula to find the distance between two points.
这篇关于Codeigniter和SQL / Haversine公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!