SET FOREIGN_KEY_CHECKS=;
-- ----------------------------
-- Table structure for customer
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`id` int() unsigned NOT NULL auto_increment COMMENT '自增主键',
`name` varchar() NOT NULL COMMENT '名称',
`lon` double(,) NOT NULL COMMENT '经度',
`lat` double(,) NOT NULL COMMENT '纬度',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8 COMMENT='商户表';
-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES ('', '天津市区', '117.315575', '39.133462');
INSERT INTO `customer` VALUES ('', '北京市区', '116.407999', '39.894073');
INSERT INTO `customer` VALUES ('', '保定', '115.557124', '38.853490');
INSERT INTO `customer` VALUES ('', '石家庄', '114.646458', '38.072369');
INSERT INTO `customer` VALUES ('', '昌平区1', '116.367180', '40.009561');
INSERT INTO `customer` VALUES ('', '海淀区2', '116.313425', '39.973078');
INSERT INTO `customer` VALUES ('', '海淀区1', '116.329236', '39.987231');
然后我们开始用mysql自带的函数,计算customer表中,每个地方具体。
传入参数 纬度 40.0497810000 经度 116.3424590000
SELECT
*,
ROUND(
6378.138 * * ASIN(
SQRT(
POW(
SIN(
(
40.0497810000 * PI() / - lat * PI() /
) /
),
) + COS(40.0497810000 * PI() / ) * COS(lat * PI() / ) * POW(
SIN(
(
116.3424590000 * PI() / - lon * PI() /
) /
),
)
)
) *
) AS juli
FROM
customer
ORDER BY
juli ASC
至此,我们就能清楚的查看到纬度 40.0497810000 经度 116.3424590000 距离customer表中的每个地区的距离(单位 m)

PS 用到的经纬度查询工具 http://www.gpsspg.com/maps.htm
原文链接:http://www.cnblogs.com/jiafuwei/p/5699091.html