简体   繁体   中英

Javascript to PHP Code translation help me?

hello what is the php equivalent of the following javascript code?

function arePointsNear(checkPoint, centerPoint, km) {
    var ky = 40000 / 360;
    var kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky;
    var dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx;
    var dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky;

    return Math.sqrt(dx * dx + dy * dy) <= km;
}

var vasteras = { lat: 41.235188, lng: 28.495035 };
var stockholm = { lat: 41.09774752058191, lng: 29.080487759810946 };

var n = arePointsNear(vasteras, stockholm, 49);

console.log(n);
function arePointsNear($checkPoint, $centerPoint, $km) {
    $ky = 40000 / 360;
    $kx = cos(pi() * $centerPoint['lat'] / 180.0) * $ky;
    $dx = abs($centerPoint['lng'] - $checkPoint['lng']) * $kx;
    $dy = abs($centerPoint['lat'] - $checkPoint['lat']) * $ky;

    return sqrt($dx * $dx + $dy * $dy) <= $km;
}

$vasteras = ['lat' => 41.235188, 'lng' => 28.495035];
$stockholm = ['lat' => 41.09774752058191, 'lng' => 29.080487759810946];

$n = arePointsNear($vasteras, $stockholm, 49);

var_dump($n);

But specialised solutions like https://github.com/mjaschen/phpgeo is preferable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM