Find nearest location using latitude longitude in php and mysql

3 minutes read

Get a list of user who are inside the circle or square of a given latitude and longitude with given distance or radius.

For example my input Lat= 75.1545 and Long = 64.7643 and distance = 10 miles. I would like to get the list of users who are inside 10 miles distance from the point 75.1545 and 64.7643. Now its possible to solve this by single Mysql query.

So we need  a user  table with user name, latitude and longitude of the users address.

The Mysql query that will find the closest 50 locations that are within a radius of 10 miles to the 75.1545, 64.7643 coordinate. This Query will calculates the distance based on the latitude/longitude of that row and the target lat/long, and then asks for only rows where the distance value is less than 10 miles, orders the whole query by distance, and limits it to 50 results.

To search by kilometers instead of miles, replace 3959 with 6371.

 

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

“How to Export MySQL data to Excel in PHP ?” This script will used to export data into .xls format from mysql database.You need to just copy this code and update database connection details. Below code will export mysql data into excel (.xls format) with ever...
What is PHP ? PHP stands for Hypertext Preprocessor and is a server-side language. PHP allows web developers to create dynamic content that interacts with databases. PHP is relatively new (compared to languages such as Perl (CGI) and Java) but is quickly becom...
Frequently asked basic and advanced MySql Queries Interview Questions and Answers. PHP Mysql Query 1. What is MySQL?  MySQL is an open source RDBMS which is built, supported and distributed by MySQL (now acquired by Oracle). 2. What is the Differentiate betwee...