-
-
Save mertbuldur/49159cf02107e92d7c6605ea0411d7ef to your computer and use it in GitHub Desktop.
DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$db = new PDO("mysql:host=localhost;dbname=coordinate;charset=utf8","root","123123asdasd"); | |
} | |
catch(PDOException $e){ | |
echo $e->getMessage(); | |
} | |
function distance($lat1,$long1,$lat2,$long2){ | |
$theta = $long1 - $long2; | |
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); | |
$dist = acos($dist); | |
$dist = rad2deg($dist); | |
$miles = $dist * 60 * 1.1515; | |
return $miles * 1.609344; | |
} | |
$latitude = 41.081207; | |
$longitude = 28.811592; | |
$query = $db->prepare("select name,latitude,longitude,(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?))+ sin(radians(?)) * sin(radians(latitude)))) as distance from data where | |
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?))+ sin(radians(?)) * sin(radians(latitude)))) < 15 | |
"); | |
$query->execute(array($latitude,$longitude,$latitude,$latitude,$longitude,$latitude)); | |
$data = $query->fetchAll(PDO::FETCH_ASSOC); | |
foreach($data as $item){ | |
echo '<li>'.$item['name'].' - '.$item['distance'].' km - '.distance($latitude,$longitude,$item['latitude'],$item['longitude']).'</li>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment