Mysql根據經緯度篩選資料
阿新 • • 發佈:2018-11-19
建立位置表,並且插入測試資料
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 80011
Source Host : localhost:3306
Source
Database
: test
Target Server Type : MYSQL
Target Server Version : 80011
File Encoding : 65001
Date
: 2018-11-07 16:58:27
*/
SET
FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for location
-- ----------------------------
DROP
TABLE
IF EXISTS `location`;
CREATE
TABLE
`location` (
`id`
INT
(10) UNSIGNED
NOT
NULL
AUTO_INCREMENT,
`
name
`
VARCHAR
(50)
NOT
NULL
,
`longitude`
DECIMAL
(13, 10)
NOT
NULL
,
`latitude`
DECIMAL
(13, 10)
NOT
NULL
,
PRIMARY
KEY
(`id`),
KEY
`long_lat_index` (`longitude`, `latitude`)
) ENGINE = INNODB AUTO_INCREMENT = 5
DEFAULT
CHARSET = utf8;
-- ----------------------------
-- Records of location
-- ----------------------------
INSERT
INTO
`location`
VALUES
(
'1'
,
'廣東省深圳市龍崗區阪田街道五和大道萬科四季花城北區'
,
'22.6265210000'
,
'114.0606880000'
);
INSERT
INTO
`location`
VALUES
(
'2'
,
'廣東省深圳市龍華區民治(地鐵站)'
,
'22.6175280000'
,
'114.0406460000'
);
INSERT
INTO
`location`
VALUES
(
'3'
,
'廣東省深圳市龍華區紅山(地鐵站)'
,
'22.6218860000'
,
'114.0234800000'
);
INSERT
INTO
`location`
VALUES
(
'4'
,
'廣東省深圳市南山區西麗街道沙河西路名典商旅酒店(深圳西麗店)'
,
'22.5801670000'
,
'113.9543000000'
);
搜尋附近50KM的資料
#經度:22.626521
#緯度:114.060688
#50KM範圍
SELECT
*
FROM
(
SELECT
*, sqrt(
(
(
(22.626521 - longitude) * PI() * 12656 * cos(
((114.060688 + latitude) / 2) * PI() / 180
) / 180
) * (
(22.626521 - longitude) * PI() * 12656 * cos(
((114.060688 + latitude) / 2) * PI() / 180
) / 180
)
) + (
(
(114.060688 - latitude) * PI() * 12656 / 180
) * (
(114.060688 - latitude) * PI() * 12656 / 180
)
)
)
AS
lc
FROM
location
) location
WHERE
lc < 50 ##距離小於50KM
ORDER
BY
##距離小於排序
lc
顯示結果