所以我试图通过php获取坐标之间的距离。 我使用这两个功能:
在ip中定位用户我从ip获取经度和纬度,并将它们放入$ lat1和$ 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";
在获取图像中我选择行,它们都包含来自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
所以在这两个函数之后我现在可以返回四个变量,其中我想计算两个纬度和经度之间的距离。
- $ lat1
- $ lon1
- $ lat2
- $ lon2
答案 0 :(得分:1)
你想看Haversine Formula我不热衷于PHP,但在伪PHP代码中就是这样:
$EarthRadius = 6371000; // radius in meters
$phi1 = deg2rad($lat1)
$phi2 = deg2rad($lat2)
$deltaLat = deg2rad($lat2 - $lat1)
$deltaLon = deg2rad($lon2 - $lon1)
var $a = sin($deltaLat/2) * sin($deltaLat/2) + cos($phi1) * cos($phi2) * sin($deltaLon / 2) * sin($deltaLon / 2);
var $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
var $result = $EarthRadius * $c;
您应该能够在PHP Math模块中找到cos,sin,atan2的等效公式。还有一些其他的近似值,但这应该工作得很好。
答案 1 :(得分:0)
来自http://rosettacode.org/wiki/Haversine_formula#PHP
+---------+---------------------+---------------------+------------------+
| Overlap | overlap_down_time | up_time | overlap_duration |
+---------+---------------------+---------------------+------------------+
| 1 | 2015-01-01 00:00:05 | 2015-01-01 00:00:06 | 00:00:01 |
+---------+---------------------+---------------------+------------------+