Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

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

    Back-End Development
    mysql database sql
    2
    3
    661
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • root
      root Linux Help last edited by

      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;
      
      1 Reply Last reply Reply Quote 1
      • dev_lak
        dev_lak last edited by

        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
        
        root 1 Reply Last reply Reply Quote 2
        • root
          root Linux Help @dev_lak last edited by

          @dev_lak

          Wow. it's working

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post

          0
          Online

          3.6k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |