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

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

问题描述

我已经有纬度和表经度






 纬度经度






  40.8151 -73.0455ū 36103 
40.8132 -73.0476ü36103

所以,我想是我传递两个经度和从我的页面纬度我怎样才能使用存储过程找到最近的地方我没有一个非常手,所以我要问这里



使用这个

 (3959 * ACOS(COS(弧度(37))* COS(弧度(@latitude))* COS(弧度(@longtitude ) - 弧度(-122))+罪(弧度(37))*罪(弧度(@latitude))))

但我必须与表中的值,以便匹配它,我怎么能做到这一点?


解决方案

  SELECT ROUND(6371 * ACOS(COS(弧度('纬度'))* COS(弧度(纬度))* COS(弧度(经度) - 弧度('长'))+罪(弧度('纬度'))*罪(弧度(纬度))))距离,经度,纬度,从your_table HAVING距离< = 20 ORDER BY距离

您可以使用类似于在SQL上面显示的查询,其中'纬度''长是要传递到搜索变量值。在'东经''纬度'是从你的表的列名。而且顺便说一句,这就是所谓的haversine公式和恒 6371 用于获取距离KM,被使用,而3959 获得英里的距离。您可以使用它们按您的要求。


I have table having latitude and longitude


     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

Tried using this one

( 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

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天全站免登陆