Get Lat and Lon of all location that are withing 1km radius of my current location through MySQL
-
Hi, I want to get locations table rows, that are withing 1km radius of my current location.
CREATE TABLE `locations` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `lat` double NOT NULL, `lng` double NOT NULL, `user_id` int(11) unsigned NOT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-
Hi, try below query, replace
YOUR_LAT
andYOUR_LNG
with your current position and changeDISTANCE_IN_MILE
as you requiredselect * from locations where lat!='' and lng!='' and ( 3959 * acos( cos( radians(YOUR_LAT) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(YOUR_LNG) ) + sin( radians(YOUR_LAT) ) * sin( radians( lat ) ) ) ) < DISTANCE_IN_MILE
-
Wow. it's working