如何使用经度和纬度计算距离? [英] How to calculate distance using latitude and longitude?

查看:126
本文介绍了如何使用经度和纬度计算距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌子有经度和纬度

     Lat            Longitude


40.8151 -73.0455    U   36103
40.8132 -73.0476    U   36103

所以我想要的是从页面上传递两个经度和纬度.如何使用存储过程找到最近的地方?我没有太多帮助,所以我必须在这里询问

So what i want is I am passing two Longitude and latitude from my page How can i find nearest place using store procedure i don't have a much hand on it so i have to ask here

尝试过使用这个

( 3959 * acos( cos( radians(37) ) * cos( radians( @latitude ) ) * cos( radians( @longtitude ) - radians(-122) ) + sin( radians(37) ) * sin( radians( @latitude ) ) ) )

但是我必须将其与表值相匹配,以便如何实现呢?

But i have to match it with the table values so how can i achieve this?

推荐答案

SELECT ROUND(6371 * acos(cos(radians('lat')) * cos(radians(latitude)) * cos(radians(longitude) - radians('long')) + sin(radians('lat')) * sin(radians(latitude)))) as distance,latitude,longitude, from your_table HAVING distance<=20  order by distance

您可以使用与上面的SQL相似的查询,其中'lat''long'是要搜索的变量值. 'longitude''latitude'是表中的列名.顺便说一句,这称为Haversine公式,常数6371用于获取以KM为单位的距离,而3959用于获取以英里为单位的距离.您可以根据需要使用它们中的任何一个.

You can use a query similar to shown above in your SQL where 'lat' and 'long' are the variable values that you are passing to search for. The 'longitude' and 'latitude' are column names from your table. And btw, this is called Haversine formula and the constant 6371 is used to get distance in KM, while 3959 is used to get distance in miles. You can use either of them as per your requirement.

这篇关于如何使用经度和纬度计算距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆