var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alrt("ระยะทางได้ผลลัพธ์ = " + d);
ผมแปลงได้แบบนี้ครับ แต่ค่า $a $c ไม่ออกครับ
<?
$R=6371;
dlat=(lat2-lat1);
dlon=(lon2-lon1);
$a=sin($dlat/2)*sin(dlat/2)+cos(lat1)*cos(lat2)*sin(dlon/2)*sin(dlon/2);
$c=2*atan2(sqrt($a),sqrt (1-$a));
$d=$R*$c;
?>
<?
$lat_prd=14.8495;
$lng_prd=103.495;
?>
<script type="text/javascript">
var lat1=<?=$lat_prd?>;
var lon1=<?=$lng_prd?>;
var R = 6371; // km
var dLat = (14.85467-lat1).toRad();
var dLon = (103.48357-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(14.85467.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alrt("ระยะทางได้ผลลัพธ์ = " + d);
</script>