Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c# - How to calculate distance using latitude and longitude?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...