Get Lat and Lon of all location that are withing 1km radius of my current location through MySQL


  • Linux Help

    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 and YOUR_LNG with your current position and change DISTANCE_IN_MILE as you required

    select * 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
    

  • Linux Help

    @dev_lak

    Wow. it's working