ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
I've been racking my brains over this for a few hours, with no joy.
Basically, I have a MySQL database that stores a series of latitude/longitude coordinates in two fields (lat and long). I need to be able to search the database and retrieve records that are within a given distance of a lat/long pair that is specified at search using PHP.
I have managed to retrieve everything that is within a set distance east/west and north/south, the only problem is I can only figure out how to get data in a square (ie lat + distance, long + distance), which means that the corners are about 41% too far, but I cant think of any way to get only the data that is within the circle.
I hope that I have managed to explain it well enough, and any help would be really appreciated,
Chris
Since you have restricted your return set to pints that are bounded by a square, I guess that you need to add one more condition to your existing conditions restricting the set of points to lie within the circle that is bounded by the square.
Use the formula a*a + b*b <= r*r, where a is the latitudinal difference, b is the longitudinal difference and r is the given distance.
I would compute the r*r once and feed it into the query rather than recomputing it for every member of the set that is being examined (I rather doubt the SELECT statement will optimize to handle this).
Also, be aware that the accuracy of this approach will vary considerably with latitude because your lat-long "square" isn't a square, it is a trapezoid. The longitudinal distances between whole degree measurements vary from about 69.5 miles (114.4 KM) at the equator to zero at either pole, while the latitude distance is constant at about 69.5 miles. At a latitude of 45 degrees, the longitudinal distance will be about 49.1 miles (80.9 KM)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.