我能够计算两点之间的距离,但是我的问题是如何从数据库中存储的多个数据中计算距离,在单行中我只能得到一个纬度和一个经度值。
要找到距离,我需要起点和终点的纬度长,但是在单行数据库中,它只有一个纬度和经度长值。
此功能可用于查找距离,但是如何将其用于数据库中的多个数据,而我在该行中没有起始值和结束值。
CREATE TABLE `distance_calculate` (
`id` int(11) NOT NULL,
`vehicle_id` int(11) NOT NULL,
`latitude` varchar(50) NOT NULL,
`longitude` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `distance_calculate` (`id`, `vehicle_id`, `latitude`, `longitude`)
VALUES(1, 1, '23.247137', '69.700527' ),
(2, 1, '23.252473', '69.685735' ),
(3, 1, '23.249922', '69.686488'),
(4, 1, '23.249045', '69.686204'),
(5, 1, '23.247704', '69.684042'),
(6, 1, '23.246255', '69.683039'),
(7, 1, '23.243978', '69.681671'),
(8, 1, '23.241415', '69.681384');
id vehicle_id latitude longitude
1 1 23.247137 69.700527
2 1 23.262473 69.686735
3 1 23.249922 69.686488
4 1 23.249045 69.686204
5 1 23.247704 69.684042
6 1 23.246265 69.683039
7 1 23.243978 69.681671
8 1 23.241415 69.681384
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
return 0;
}else {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
}
答案 0 :(得分:0)
这是根据要求的JOIN演示...
SELECT x.*
, y.id target_id
, y.latitude target_lat
, y.longitude target_long
FROM distance_calculate x
JOIN distance_calculate y
ON y.id < x.id;
+----+------------+-----------+-----------+-----------+------------+-------------+
| id | vehicle_id | latitude | longitude | target_id | target_lat | target_long |
+----+------------+-----------+-----------+-----------+------------+-------------+
| 2 | 1 | 23.252473 | 69.685735 | 1 | 23.247137 | 69.700527 |
| 3 | 1 | 23.249922 | 69.686488 | 1 | 23.247137 | 69.700527 |
| 4 | 1 | 23.249045 | 69.686204 | 1 | 23.247137 | 69.700527 |
| 5 | 1 | 23.247704 | 69.684042 | 1 | 23.247137 | 69.700527 |
| 6 | 1 | 23.246255 | 69.683039 | 1 | 23.247137 | 69.700527 |
| 7 | 1 | 23.243978 | 69.681671 | 1 | 23.247137 | 69.700527 |
| 8 | 1 | 23.241415 | 69.681384 | 1 | 23.247137 | 69.700527 |
| 3 | 1 | 23.249922 | 69.686488 | 2 | 23.252473 | 69.685735 |
| 4 | 1 | 23.249045 | 69.686204 | 2 | 23.252473 | 69.685735 |
| 5 | 1 | 23.247704 | 69.684042 | 2 | 23.252473 | 69.685735 |
| 6 | 1 | 23.246255 | 69.683039 | 2 | 23.252473 | 69.685735 |
| 7 | 1 | 23.243978 | 69.681671 | 2 | 23.252473 | 69.685735 |
| 8 | 1 | 23.241415 | 69.681384 | 2 | 23.252473 | 69.685735 |
| 4 | 1 | 23.249045 | 69.686204 | 3 | 23.249922 | 69.686488 |
| 5 | 1 | 23.247704 | 69.684042 | 3 | 23.249922 | 69.686488 |
| 6 | 1 | 23.246255 | 69.683039 | 3 | 23.249922 | 69.686488 |
| 7 | 1 | 23.243978 | 69.681671 | 3 | 23.249922 | 69.686488 |
| 8 | 1 | 23.241415 | 69.681384 | 3 | 23.249922 | 69.686488 |
| 5 | 1 | 23.247704 | 69.684042 | 4 | 23.249045 | 69.686204 |
| 6 | 1 | 23.246255 | 69.683039 | 4 | 23.249045 | 69.686204 |
| 7 | 1 | 23.243978 | 69.681671 | 4 | 23.249045 | 69.686204 |
| 8 | 1 | 23.241415 | 69.681384 | 4 | 23.249045 | 69.686204 |
| 6 | 1 | 23.246255 | 69.683039 | 5 | 23.247704 | 69.684042 |
| 7 | 1 | 23.243978 | 69.681671 | 5 | 23.247704 | 69.684042 |
| 8 | 1 | 23.241415 | 69.681384 | 5 | 23.247704 | 69.684042 |
| 7 | 1 | 23.243978 | 69.681671 | 6 | 23.246255 | 69.683039 |
| 8 | 1 | 23.241415 | 69.681384 | 6 | 23.246255 | 69.683039 |
| 8 | 1 | 23.241415 | 69.681384 | 7 | 23.243978 | 69.681671 |
+----+------------+-----------+-----------+-----------+------------+-------------+
我的数据库中有一个函数可以计算地球坐标之间的距离(以km为单位)。看起来像这样:
delimiter //
create DEFINER = CURRENT_USER function geo_distance_km (lat1 double, lon1 double, lat2 double, lon2 double) returns double
begin
declare R int DEFAULT 6372.8;
declare phi1 double;
declare phi2 double;
declare d_phi double;
declare d_lambda double;
declare a double;
declare c double;
declare d double;
set phi1 = radians(lat1);
set phi2 = radians(lat2);
set d_phi = radians(lat2-lat1);
set d_lambda = radians(lon2-lon1);
set a = sin(d_phi/2) * sin(d_phi/2) +
cos(phi1) * cos(phi2) *
sin(d_lambda/2) * sin(d_lambda/2);
set c = 2 * atan2(sqrt(a), sqrt(1-a));
set d = R * c;
return d;
end;
//
delimiter ;
您可以像这样使用它:
SELECT x.id source_id
, y.id target_id
, geo_distance_km(x.latitude,x.longitude,y.latitude,y.longitude) delta
FROM distance_calculate x
JOIN distance_calculate y
ON y.id < x.id;
+-----------+-----------+------------------+
| source_id | target_id | delta |
+-----------+-----------+------------------+
| 2 | 1 | 1.6240400586719 |
| 3 | 1 | 1.4678198641286 |
| 4 | 1 | 1.4790932430399 |
| 5 | 1 | 1.6859300396863 |
| 6 | 1 | 1.7899557954697 |
| 7 | 1 | 1.9588626645576 |
| 8 | 1 | 2.0573621504414 |
| 3 | 2 | 0.29399727258978 |
| 4 | 2 | 0.38429654604457 |
| 5 | 2 | 0.55795904605742 |
| 6 | 2 | 0.74448757446836 |
| 7 | 2 | 1.0321499787366 |
| 8 | 2 | 1.307891566912 |
| 4 | 3 | 0.10177484098073 |
| 5 | 3 | 0.35121611491034 |
| 6 | 3 | 0.53908239576653 |
| 7 | 3 | 0.82430155029471 |
| 8 | 3 | 1.0804876797252 |
| 5 | 4 | 0.26658679033431 |
| 6 | 4 | 0.44825363131558 |
| 7 | 4 | 0.72956618879477 |
| 8 | 4 | 0.98128776490424 |
| 6 | 5 | 0.19100754800257 |
| 7 | 5 | 0.48008313462389 |
| 8 | 5 | 0.75041889106272 |
| 7 | 6 | 0.28929719254935 |
| 8 | 6 | 0.5642986247924 |
| 8 | 7 | 0.28658708452204 |
+-----------+-----------+------------------+
因此,从Thakarji Lodge环形交叉路口到火车站环路,我们可以看到它很短暂