So i'm trying to get the distance between to coordinates using php. I'm using these two functions:
- locate user by ip
- get image with gps coordinates
in locate user by ip i'm getting the latitude and longitude from the ip, and the putting them in $ lat1 and $ lon1
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
$user_location = $details->loc;
$pieces = explode(",", $user_location);
$lat1 = $pieces[0];
$lon1 = $pieces[1];
$unit = "Km";
in get image i'm selecting the rows, and they all contain the latitude and longitude from the exif.
function get_image($db){
$select = "id, image_name";
$sql = "SELECT $select FROM images ORDER BY id DESC";
$stmt = $db->prepare($sql);
$stmt->execute();
$spot = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(!$stmt -> rowCount()){
echo "<div class='noSpots'>
<p>Sorry there seams to be nothing in your area</p>
</div>";
}
return $spot;
}//spots_narrow ends here
so after these two functions I can now return four variables with the two latitudes and longitudes that i want to calculate the distance between.
- $ lat1
- $ lon1
- $ lat2
- $ lon2