创建函数
.....
- DROP FUNCTION IF EXISTS GeoDistDiff;
- DELIMITER |
- CREATE FUNCTION GeoDistDiff( type ENUM('mi', 'km'), lat1 DECIMAL(10,7), lon1 DECIMAL(10,7), lat2 DECIMAL(10,7), lon2 DECIMAL(10,7) ) RETURNS DECIMAL(10,7)
- BEGIN
- RETURN ( IF(type = 'km', 6371, 3959) * acos( cos( radians(lat2) ) * cos( radians( lat1 ) ) * cos( radians( lon1 ) - radians(lon2) ) + sin( radians(lat2) ) * sin( radians( lat1 ) ) ) );
- END;
- |
- DELIMITER ;
使用方法
- SELECT
- GeoDistDiff('km', 50.459634, -3.526109, 50.459408, -3.524457) as distance_km,
- GeoDistDiff('mi', 50.459634, -3.526109, 50.459408, -3.524457) as distance_mi;
- +---------------------+---------------------+
- | distance_km | distance_mi |
- +---------------------+---------------------+
- | 0.11959195137023926 | 0.07431557774543762 |
- +---------------------+---------------------+
- 1 row in set (0.00 sec)
转载请注明:谷谷点程序 » mysql算出经纬度的距离