0

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);
1
  • abs(-4.2); for Math.abs. cos(M_PI); for Math.cos, sqrt(9); for Math.sqrt. php.net/manual/en/function.sqrt.php All searchable here. Commented Jun 25, 2019 at 22:02

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.