MySQL自5.7版本以后支持如下与本文相关的GIS特征:
- InnoDB的位置索引
- 计算地球距离的原生函数
首先创建表:
CREATE TABLE geo_test (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
geo GEOMETRY NOT NULL,
PRIMARY KEY (id));
插入数据:
insert into geo_test (name, geo) values
('黄山菜饭', POINT(135.2365,36.4164)),
('沙县小吃', POINT(135.3365,36.4164)),
('兰州拉面', POINT(135.4365,36.4164));
计算距离
select name, st_distance_sphere(geo, POINT(135.4365,36.4164)) from geo_test;
输出结果如下
'黄山菜饭','17896.200649484592'
'沙县小吃','8948.101525501479'
'兰州拉面','0'
以上仅为几个简单的例子,要点为 st_distance_sphere 函数,返回结果为两点之间的距离,单位为米
参考: