03-Apr-2023
.
Admin
Hi Dev,
This tutorial is focused on how to find the nearest location using latitude and longitude in PHP. you'll learn how to find the nearest location using latitude and longitude using PHP. We will use how to find the nearest location using longitude and latitude using PHP. if you have a question about how to find the nearest location using longitude and latitude in PHP then I will give a simple example with a solution.
Now, let's see the article on how to find the nearest location using longitude and latitude in PHP. it's a simple example of how to find the nearest location using longitude and latitude using PHP. you can understand the concept of how to find the nearest location using latitude and longitude using PHP. if you have a question about how to find the nearest location using latitude and longitude in PHP then I will give a simple example with a solution.
Step 1: Connecting to MySQL database
<?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
<!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
<!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>
I hope it could help you...
#PHP