Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

MySQL query for WGS-84 longitude & latitude radius GPS distance calculation
WGS-84 Proximity Search with Longitude & Latitude decimal co-ordinates

Example: Hotel Burnham in Downtown Chicago is located at:
1 West Washington Street, Chicago, IL
Latitude: 41.883190
Longitude: -87.627940

I absolutely want to help some people out there with the same problem I
faced. I spent all night scanning lines of code trying SQL and C# code
that simply just DOES NOT work. There is TONS of crap code scattered
all over the internet, but very many great articles explaining how
everything works, so I piggybacked on the knowledge out there to get a
working solution. For those like me, you just want to use the code and
hit the ground running.

The basis of my calculation is derived from a Microsoft White Paper.

So here goes, d = distance, is the final output of this equation. R =
the earth's radius (and can be in m, km, mi or nm) The values 'a', 'b'
and 'c' are just steps to break the formula into digestable chunks:

a = sin(lat1)*sin(lat2)
b = cos(lat1)*cos(lat2)*cos(lon2-lon1)
c = acos(a+b)
d = R*c

The earth's radius is:
6378137 meters
6378.137 km
3963.191 miles
3441.596 nautical miles

Simply choose the value for R, for your desired distance results.
Example: R = 3963.191 (OR d = 3963.191*c) will return distance from
main point of interest in miles, use 6378.137 instead as the R
parameter and get a result in KM's

Other constants, MySQL actually has a function called "PI()" which I
will use in this example, but its precision is not quite as great as
the number below:
PI = 3.141592653589793

SELECT 3963.191 * ACOS( (SIN(PI()*41.883190/180)*SIN(PI()*MapLat/180))
+
(COS(PI()*41.883190/180)*cos(PI()*MapLat/180)*COS(PI()*MapLong/180-PI()*-87.
627940/180))
), Company, Address FROM boomtrek.poi WHERE City = 'Chicago'

The fields in my database are MapLat and MapLong for the decimal
degrees values that work with Google Maps WGS-84 standard. I found that
the majority of tutorials out there neglected to give much info on the
parameters being passed into the method, just simply the method.

NOTE: all latitude/longitude inputs must be converted into RADIANS!!

The calculate for decimal degrees like 'lat1' (41.883190) to radians
is:
PI() * lat1 / 180
OR
3.14159 * 41.883190 / 180

lon1 input is like this:
PI() * -87.627940 / 180
....to convert decimal degrees into radians.

lat1 & lon1 input parms - should be the fixed point where you want to
search proximity, this is the point of interest to find things nearby
it, in this case:
(1 West Washington Street, Chicago, IL)
Latitude: 41.883190
Longitude: -87.627940

MapLat = lat2 input parameter (MapLat = the MySQL field in the database
for the latitude geocoding)
MapLong = lon2 input parameter (MapLong is the MySQL longitude field in
the database)

For Mapping; tacking the same stuff as the SELECT clause on to the
WHERE clause one can find points within a 1.5 mile radius, and reduce
the resultset size, displaying all points on one map page, between
10-25 points should be a great number.

AND 3963.191 * ACOS( (SIN(PI()*41.883190/180)*SIN(PI()*MapLat/180)) +
(COS(PI()*41.883190/180)*cos(PI()*MapLat/180)*COS(PI()*MapLong/180-PI()*-87.
627940/180))
) <= 1.5 LIMIT 25

And finally; using an ORDER BY with the same stuff yet again you could
find the things closet to your proximity where you're standing or
driving:

ORDER BY 3963.191 * ACOS(
(SIN(PI()*41.883190/180)*SIN(PI()*MapLat/180)) +
(COS(PI()*41.883190/180)*cos(PI()*MapLat/180)*COS(PI()*MapLong/180-PI()*-87.
627940/180))
)

I really hope this was of use to some people, you can see the mapping
and proximity searching in action at Boomtrek Local Search:
http://www.boomtrek.com/search.aspx...ww.boomtrek.com
boomtrek@hotmail.com (MSN)


Report this thread to moderator Post Follow-up to this message
Old Post
boomtrek@hotmail.com
11-21-05 09:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

SQL Server Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:16 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.