Php 如何利用多纬度经度数据计算车辆行驶距离
我能够计算两点之间的距离,但我的问题是如何计算数据库中存储的多个数据之间的距离,而在单行中,我只能得到一个纬度和一个经度值 为了找到距离,我需要起点和终点的lat long,但在单行中,它只有一个lat和long值 此函数用于查找距离,但如何将其用于数据库中的多个数据,而这些数据在单行中没有起始值和结束值Php 如何利用多纬度经度数据计算车辆行驶距离,php,mysql,Php,Mysql,我能够计算两点之间的距离,但我的问题是如何计算数据库中存储的多个数据之间的距离,而在单行中,我只能得到一个纬度和一个经度值 为了找到距离,我需要起点和终点的lat long,但在单行中,它只有一个lat和long值 此函数用于查找距离,但如何将其用于数据库中的多个数据,而这些数据在单行中没有起始值和结束值 CREATE TABLE `distance_calculate` ( `id` int(11) NOT NULL, `vehicle_id` int(11) NOT NULL,
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');
这是一个连接的演示,按要求
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 |
+----+------------+-----------+-----------+-----------+------------+-------------+
您可以这样使用它:
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 |
+-----------+-----------+------------------+
选择x.id源\u id
,y.id目标\ u id
,地理距离公里(x纬度,x经度,y纬度,y经度)增量
从距离_计算x
连接距离_计算y
y.id
因此,我们可以看到,从Thakarji Lodge环岛到火车站环路的路程很短是的,我知道连接。请告诉我如何使用连接实现连接。你能给我看一下示例吗?sql fiddle查询的链接上面写着显示多个重复记录。这里没有重复记录。
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 |
+-----------+-----------+------------------+