mysql 下 計算 兩點 經緯度 之間的距離 含具體sql語句

來源:互聯網
上載者:User

標籤:

文章轉載地址 http://blog.sina.com.cn/s/blog_7bbfd5fd01017d1e.html 感謝作者。

在原文的基礎上,我新增了sql語句,方便大家理解

mysql距離計算,單位m,以及排序lon 經度 lat 緯度一般地圖上顯示的座標順序為,緯度在前(範圍-90~90),經度在後(範圍-180~180)首先建立一張表,裡麵包含經緯度
SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for customer-- ----------------------------DROP TABLE IF EXISTS `customer`;CREATE TABLE `customer` (  `id` int(11) unsigned NOT NULL auto_increment COMMENT ‘自增主鍵‘,  `name` varchar(50) NOT NULL COMMENT ‘名稱‘,  `lon` double(9,6) NOT NULL COMMENT ‘經度‘,  `lat` double(8,6) NOT NULL COMMENT ‘緯度‘,  PRIMARY KEY  (`id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT=‘商戶表‘;-- ------------------------------ Records of customer-- ----------------------------INSERT INTO `customer` VALUES (‘1‘, ‘天津市區‘, ‘117.315575‘, ‘39.133462‘);INSERT INTO `customer` VALUES (‘2‘, ‘北京市區‘, ‘116.407999‘, ‘39.894073‘);INSERT INTO `customer` VALUES (‘3‘, ‘保定‘, ‘115.557124‘, ‘38.853490‘);INSERT INTO `customer` VALUES (‘4‘, ‘石家莊‘, ‘114.646458‘, ‘38.072369‘);INSERT INTO `customer` VALUES (‘5‘, ‘昌平區1‘, ‘116.367180‘, ‘40.009561‘);INSERT INTO `customer` VALUES (‘6‘, ‘海澱區2‘, ‘116.313425‘, ‘39.973078‘);INSERT INTO `customer` VALUES (‘7‘, ‘海澱區1‘, ‘116.329236‘, ‘39.987231‘);

  然後我們開始用mysql內建的函數,計算customer表中,每個地方具體。

  傳入參數 緯度 40.0497810000 經度 116.3424590000
/*傳入的參數為  緯度 緯度 經度 ASC升序由近至遠 DESC 降序 由遠到近 */SELECT    *,    ROUND(        6378.138 * 2 * ASIN(            SQRT(                POW(                    SIN(                        (                            40.0497810000 * PI() / 180 - lat * PI() / 180                        ) / 2                    ),                    2                ) + COS(40.0497810000 * PI() / 180) * COS(lat * PI() / 180) * POW(                    SIN(                        (                            116.3424590000 * PI() / 180 - lon * PI() / 180                        ) / 2                    ),                    2                )            )        ) * 1000    ) AS juliFROM    customerORDER BY    juli ASC

  至此,我們就能清楚的查看到緯度 40.0497810000 經度 116.3424590000 距離customer表中的每個地區的距離(單位 m)

PS 用到的經緯度查詢工具 http://www.gpsspg.com/maps.htm

 

mysql 下 計算 兩點 經緯度 之間的距離 含具體sql語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.