Find Nearest Location using Latitude Longitude PHP + MySQL

To find nearest location using latitude and longitude in PHP + MySQL; In this tutorial, i am going to show you how to find/get nearest location using latitude and longitude in PHP + MySQL.

How to find & display nearest location using latitude and longitude in PHP + MySQL example tutorial, i will create one form, which take latitude and longitude. Then create another PHP file, which is used to find & display nearest location using latitude and longitude From MySQL database in PHP.

PHP + MySQL Find Nearest Location using Latitude and Longitude

  • Step 1 – Create PHP Project
  • Step 2 – Connect App to Database
  • Step 3 – Create Take Latitude and Longitude Form
  • Step 4 – Find and Display Nearest Location using Latitude and Longitude

Step 1 – Create PHP Project

In step 1, Navigate to your local web server directory. And inside this directory, create one directory. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Connect App to Database

Then create a php file that named mydbCon. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon file and add the below given code into your file:

<?php

    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $conn= mysqli_connect($hName,$uName,$password,"$dbName");

      if(!$conn){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Step 3 – Create Take Latitude and Longitude Form

In step 3, create a php file that named index.php. This file will display PHP simple form and store form data into MySQL database with validation. Now, you need to add the following simple form code into index.php 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 - Laratutorials.com</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 4 – Find and Display Nearest Location using Latitude and Longitude

In step 4, create one php file, which name location.php. And add the following code into it:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Find and Display Nearest Location using Latitude and Longitude- Laratutorials.com</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>

The location.php file code will find nearest location using latitude and longitude from MySQL database in PHP. And display nearest location using latitude and longitude into HTML in PHP.

Conclusion

How to find & display nearest location using latitude and longitude in PHP + MySQL example tutorial, You have leaned how to find and display nearest location using latitude and longitude in PHP + MySQL.

Recommended PHP Tutorials

Leave a Comment