function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { $theta = $longitude1 - $longitude2; $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); $miles = acos($miles); $miles = rad2deg($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet / 3; $kilometers = $miles * 1.609344; $meters = $kilometers * 1000; return compact(‘miles‘,‘feet‘,‘yards‘,‘kilometers‘,‘meters‘); } $point1 = array(‘lat‘ => 40.770623, ‘long‘ => -73.964367); $point2 = array(‘lat‘ => 40.758224, ‘long‘ => -73.917404); $distance = getDistanceBetweenPointsNew($point1[‘lat‘], $point1[‘long‘], $point2[‘lat‘], $point2[‘long‘]); var_dump($distance); function rad($d) { return $d * 3.1415926535898 / 180.0; } function GetDistance($lat1, $lng1, $lat2, $lng2) { $EARTH_RADIUS = 6378.137; $radLat1 = rad($lat1); //echo $radLat1; $radLat2 = rad($lat2); $a = $radLat1 - $radLat2; $b = rad($lng1) - rad($lng2); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2))); $s = $s *$EARTH_RADIUS; $s = round($s * 10000) / 10000; return $s; } var_dump(GetDistance($point1[‘lat‘], $point1[‘long‘], $point2[‘lat‘], $point2[‘long‘])); /* array(5) { ["miles"]=> float(2.6025445138617) ["feet"]=> float(13741.43503319) ["yards"]=> float(4580.4783443965) ["kilometers"]=> float(4.1883893981162) ["meters"]=> float(4188.3893981162) }*/
时间: 2024-10-11 12:28:51