gpt4 book ai didi

mysql - 如何使用 MySQL 8 ST_Distance 计算两个坐标(来自谷歌地图)之间的距离?

转载 作者:行者123 更新时间:2023-11-29 16:41:09 33 4
gpt4 key购买 nike

我的数据库中有一个表,它存储谷歌地图中的位置以及它们返回的坐标。之前我将 lat 和 lng 存储为小数点,在我将 MySQL 版本升级到 8 后,发现有预定义的数据类型和函数来处理坐标和距离计算。我创建了如下表格,并插入了来自谷歌地图的一些随机值。

CREATE TABLE `Location` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lat` varchar(50) NOT NULL,
`lng` varchar(50) NOT NULL,
`coordinates` point NOT NULL,
`name` varchar(500) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_lat_lng` (`lat`,`lng`),
SPATIAL KEY `sk_coordinates` (`coordinates`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入的示例数据

INSERT INTO Location (name, lat, lng, coordinates)
SELECT 'Thayyeni Koomban', '12.33984592973732', '75.41285991668701', POINT(12.33984592973732, 75.41285991668701) UNION
SELECT 'Tejaswini river rafting', '12.29208143873455', '75.4130744934082', POINT(12.29208143873455, 75.4130744934082) UNION
SELECT 'Mossy Forest, Cameron Highlands', '4.5242225', '101.38192709999998', POINT(4.5242225, 101.38192709999998) UNION
SELECT 'Dhanushkodi Point', '9.152226599999999', '79.44291569999996', POINT(9.152226599999999, 79.44291569999996) UNION
SELECT 'KL Sentral', '3.13333', '101.68667000000005', POINT(3.13333, 101.68667000000005) UNION
SELECT 'Kozhippara Waterfalls', '11.3537295', '76.10803290000001', POINT(11.3537295, 76.10803290000001) UNION
SELECT 'Laguna Honda Hospital and Rehabilitation Center', '37.7492806', '-122.45702240000003', POINT(37.7492806, -122.45702240000003) UNION
SELECT 'Singapore Zoo', '1.4043485', '103.79302299999995', POINT(1.4043485, 103.79302299999995) UNION
SELECT 'Taj Mahal', '27.1750151', '78.04215520000002', POINT(27.1750151, 78.04215520000002) UNION
SELECT 'Sea View Point', '11.2643567', '75.76153939999995', POINT(11.2643567, 75.76153939999995) UNION
SELECT 'Club Mahindra Ashtamudi', '8.965749299999997', '76.57136119999996', POINT(8.965749299999997, 76.57136119999996) UNION
SELECT 'Kollam Beach', '8.8756778', '76.58891629999994', POINT(8.8756778, 76.58891629999994) UNION
SELECT 'Tropical Islands', '52.03892399999999', '13.748616999999967', POINT(52.03892399999999, 13.748616999999967) UNION
SELECT 'Taroko National Park', '24.15870679999999', '121.62162969999997', POINT(24.15870679999999, 121.62162969999997) UNION
SELECT 'Lake Tyrrell', '-35.2553505', '142.8419824', POINT(-35.2553505, 142.8419824) UNION
SELECT 'Karthika Regency', '10.0192948', '76.30539509999994', POINT(10.0192948, 76.30539509999994) UNION
SELECT 'Sentosa', '1.2494041', '103.83032090000006', POINT(1.2494041, 103.83032090000006) UNION
SELECT 'Kovalam Beach', '12.7902597', '80.25390390000007', POINT(12.7902597, 80.25390390000007) UNION
SELECT 'Torres del Paine National Park', '-50.9423262', '-73.40678789999998', POINT(-50.9423262, -73.40678789999998) UNION
SELECT 'Niagara Falls', '43.0828162', '-79.07416289999998', POINT(43.0828162, -79.07416289999998);

了解 Google 坐标使用 SRID 3857,因此使用不同的纬度和经度组合创建以下查询来生成点。但这些距离似乎都不准确。

SELECT 
name,
lat,
lng,
ST_Distance(ST_SRID(coordinates, 3857), ST_GeomFromText('POINT(10.0120262 76.3586236)', 3857)) AS distance,
ST_Distance(ST_SRID(POINT(lat, lng), 3857), ST_GeomFromText('POINT(10.0120262 76.3586236)', 3857)) AS lat_lng,
ST_Distance(ST_SRID(POINT(lng, lat), 3857), ST_GeomFromText('POINT(10.0120262 76.3586236)', 3857)) AS lng_lat_first,
ST_Distance(ST_SRID(POINT(lng, lat), 3857), ST_GeomFromText('POINT(76.3586236 10.0120262)', 3857)) AS lng_lat_all,
ST_Distance(ST_SRID(POINT(lng, lat), 3857), ST_SRID(POINT(76.3586236, 10.0120262), 3857)) AS lng_lat_all_x,
ST_Distance(ST_SRID(POINT(lng, lat), 3857), ST_SRID(POINT(10.0120262, 76.3586236), 3857)) AS lng_lat_all_y,
ST_Distance(ST_SRID(POINT(lat, lng), 3857), ST_SRID(POINT(10.0120262, 76.3586236), 3857)) AS lng_lat_all_z
from test.Location;

Result

我是否以正确的方式进行操作或遗漏了什么?提前致谢。

最佳答案

我不确定。但是 ST_Distance 采用两个参数。这里是三个参数。示例是

mysql> SET @g1 = Point(1,1);
mysql> SET @g2 = Point(2,2);
mysql> SELECT ST_Distance(@g1, @g2); .

关于mysql - 如何使用 MySQL 8 ST_Distance 计算两个坐标(来自谷歌地图)之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53332135/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com