[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

php mysql lat long query

Posted on 2007-03-29
3
Medium Priority
?
1,422 Views
Last Modified: 2013-12-13
I would like to search for records within a given distance like 10km.  i have a table called users with username, userid, lat and lon as fields.

I would like to return all the users within 10km from say user number 15 and then order the records from nearest to farthest.  I would like to do this in the most economic way possible so I believe that means doing all the calculations within the mysql query.

does anyone know how to accomplish this this?
0
Comment
Question by:jimfrith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18819930
Here is a PHP method to calculate distance based on latitude and longitude. There may be a MySQL way to do this, but I'm not sure:

Takes two sets of coordinates in decimal longitude and latitude format and returns the distance in kilometers.
<?php

$lat1 = 46.2341938036161;
$lon1 = -63.124778021257015;

$lat2 = 46.4141938036161;
$lon2 = -63.118571124705284;

$distance = (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);

print($distance);

?>
0
 

Author Comment

by:jimfrith
ID: 18820773
Thats not what I'm looking for.  already know the distance in kilometers I need an mysql query to return all users within that distance.
0
 

Accepted Solution

by:
jimfrith earned 0 total points
ID: 18822517
I found a tutorial here that solved my problem:

http://www.phparch.com/sample.php?disc_show=316&mid=9
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question