English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Queries are not my strong point and I hate to ask but I would like to see if anyone here could write the basic code of the syntax for me. I have a single table that has a list of US geographic locations by LAT and LONG and I need a query to return all locations within 100 miles.

txtLong,txtLat,txtCity,txtState
-92.35781,36.329026,Mountain Home,AR
-98.69707,36.804875,Alva,OK
-98.28297,36.949448,Amorita,OK
-124.35756,42.813437,Sixes,OR
-123.01769,44.061057,Springfield,OR
-75.32755,39.890462,Folsom,PA
-75.20374,40.13477,Fort Washington,PA
-96.54327,31.752538,Tehuacana,TX
-98.10754,29.699844,Canyon Lake,TX
-98.17653,29.749204,Canyon Lake,TX
-97.60044,29.269305,Smiley,TX
-81.50071,37.677563,Sabine,WV
-81.21634,37.964622,Scarbro,WV
-80.86194,38.653616,Clem,WV
-80.79366,38.704457,Gassaway,WV
-90.70102,46.273618,Mellen,WI
-90.05754,46.183572,Mercer,WI
-91.81795,46.233591,Gordon,WI

I’m writing a logistics app for our internal users and this is the final component I need

2007-10-23 01:58:25 · 3 answers · asked by Andrew H 1 in Computers & Internet Programming & Design

Its messy but after a number of reseach, I found this derived and make this inline forumila:

((ACOS(((SIN(Latitude1*3.14159265358979 / 180)*SIN(Latitude2*3.14159265358979 / 180))+(COS(Latitude1*3.14159265358979 / 180)* COS(Latitude2*3.14159265358979 / 180))) *COS((Logitude1-Logitude2)*3.14159265358979 / 180)) * 180 / 3.14159265358979) * 60 * 1.1515)

2007-10-23 11:11:45 · update #1

3 answers

This seems to be what you want

2007-10-23 02:03:24 · answer #1 · answered by AnalProgrammer 7 · 0 3

Well, from a given lattitude / longitude coordinate pair, you get a circle as your shape for all points within 100 miles. I'm afraid SQL Server is not capable of doing this, not that I'm aware of. SQL likes absolute queries, not fuzzy ones, or ones based on geometric definitions.

You could do it brute force and check if a given point is contained within the circle. To do this, calculate the distance from the input point to the desired point using pythagoras' theorem:

if (sqrt(InputPoint.Lattitude * MountainHome.Lattitude + InputPoint.Longitude * MountainHome.Longitude) < distance_to_check)
{
// point is within circle
}

For a large number of points, this is going to be terribly slow, so I recommend not taking the square root and instead squaring your distance to check (so instead of taking sqrt, you just square 100, this balances the equation and avoids the expensive square root for each point and substitutes it with a one time multiplication).

2007-10-23 12:30:48 · answer #2 · answered by Pfo 7 · 0 1

Check the google maps api, there may be a function there already.

http://www.google.com/apis/maps/

2007-10-23 09:03:47 · answer #3 · answered by Anonymous · 0 2

fedest.com, questions and answers