Reputation: 13801
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?
Upvotes: 2
Views: 8890
Reputation: 631
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.
Upvotes: 13