Find nearest location stored in db mysql based location geolocation.js

i’m newbie in my sql n php, previously i had read the article related this topic “find nearest location”, but i am little bit understand about it. i want to ask about find nearest location with haversine formula based my geolocation here is my script geolocation :

var x = document.getElementById("demo"); 

function getLocation() {
    if (navigator.geolocation) {
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
                  "<br>Longitude: " + position.coords.longitude; 

i have stored location in my db SQL latitude and longitude poi name “lat” n “lon” i want to find nearest location from my location with round distance and show the result in the table.

this is an example in other article

* Use the Haversine Formula to display the 100 closest matches to $origLat, $origLon
* Only search the MySQL table $tableName for matches within a 10 mile ($dist) radius.
include("./assets/db/db.php"); // Include database connection function
$db = new database(); // Initiate a new MySQL connection
$tableName = "db.table";
$origLat = 42.1365;
$origLon = -71.7559;
$dist = 10; // This is the maximum distance (in miles) away from $origLat, $origLon in which to search
$query = "SELECT name, latitude, longitude, 3956 * 2 * 
      ASIN(SQRT( POWER(SIN(($origLat - abs(latitude))*pi()/180/2),2)
      +COS($origLat*pi()/180 )*COS(abs(latitude)*pi()/180)
      as distance FROM $tableName WHERE 
      longitude between ($origLon-$dist/abs(cos(radians($origLat))*69)) 
      and ($origLon+$dist/abs(cos(radians($origLat))*69)) 
      and latitude between ($origLat-($dist/69)) 
      and ($origLat+($dist/69)) 
      having distance < $dist ORDER BY distance limit 100;"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo $row['name']." > ".$row['distance']."<BR>";

how to change $origLat as my geolocation Latitude (position.coords.latitude) and $origLon as my geolocation Longitude (position.coords.longitude)? Please show me an example… Many Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.