PHP Find Nearest Location using Latitude and Longitude Example
Php 26-Mar-2021

PHP Find Nearest Location using Latitude and Longitude Example

How to find nearest location using latitude and longitude in php. In this tutorial, you will learn how to find nearest location using latitude and longitude in php.

Sometimes, you need to get nearest location using latitude and longitude in PHP application. So, this tutorial will help you step by step on how to find nearest location using latitude and longitude in php.

How to Find Nearest Location using Latitude and Longitude In PHP

If you want to create find nearest location using latitude and longitude in php app. So, follow the following steps:

  • Step 1 – Connecting to MySQL database
  • Step 2 – Create Get Latitude and Longitude Form
  • Step 3 – Display Nearest Locations in Table

Step 1 – Connecting to MySQL database

In this step, you will create a file name db.php and update the below code into your file.

The following code is used to create a MySQL database connection in PHP. And also, you can use this php code for fetch, insert, update or delete records from MySQL database with and without ajax:

<?php
    $host='localhost';
    $username='root';
    $password='';
    $dbname = "my_db";
    $conn=mysqli_connect($host,$username,$password,"$dbname");
    if(!$conn)
        {
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>

Step 2 – Create Get Latitude and Longitude Form

In this step, create form to get latitude and longitude form user.

So, create index.php file and add the following code into file:

<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   
      <title>How to Find Nearest Location using Latitude and Longitude In PHP</title>
       <!-- CSS -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container mt-5">
          <div class="card">
            <div class="card-header text-center">
              How to Find Nearest Location using Latitude and Longitude In PHP
            </div>
            <div class="card-body">
              <form action="location.php" method="post">
 
                <div class="form-group">
                  <label for="exampleInputEmail1">Latitude</label>
                  <input type="text" name="lat" class="form-control" required="">
                </div>                                
 
                <div class="form-group">
                  <label for="exampleInputEmail1">Longitude</label>
                  <input type="text" name="long" class="form-control" required="">
                </div>
 
                <input type="submit" name="submit" class="btn btn-primary">
              </form>
            </div>
          </div>
      </div>
 
   </body>
</html>

Step 3 – Display Nearest Locations in Table

In this step, fetch data nearest location using lat and long from the database. And display locations in html table.

So, create location.php and add the following code into it:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Find Nearest Location using Latitude and Longitude Example</title>
</head>
<body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12 mx-auto">
                    <div class="page-header clearfix">
                        <h2 class="pull-left">Location List</h2>
                    </div>
                    <?php
 
                       require_once "db.php";
 
                        if (isset($_POST['submit'])) {
 
                            $lat = $_POST['lat'];
                            $long = $_POST['long'];
 
                            $sql = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( $lat - LatOnTable) *  pi()/180 / 2), 2) +COS( $lat * pi()/180) * COS(LatOnTable * pi()/180) * POWER(SIN(( $long - LongOnTable) * pi()/180 / 2), 2) ))) as distance  
                                from locations  
                                having  distance <= 10 
                                order by distance";
 
                            $result = mysqli_query($conn, $sql);
 
                        }
                    ?>
 
 
                    <?php
                    if (mysqli_num_rows($result) > 0) {
                    ?>
                      <table class='table table-bordered table-striped'>
                       
                      <tr>
                        <td>Address</td>
                      </tr>
                    <?php
                    $i=0;
                    while($row = mysqli_fetch_array($result)) {
                    ?>
                    <tr>
                        <td><?php echo $row["address"]; ?></td>
                    </tr>
                    <?php
                    $i++;
                    }
                    ?>
                    </table>
                     <?php
                    }
                    else{
                        echo "No result found";
                    }
                    ?>
 
                </div>
            </div>     
        </div>
 
</body>
</html>

Conclusion

How to find the nearest location using latitude and longitude in PHP. In this tutorial, you have learned how to find the nearest location using latitude and longitude in PHP.