roslov/location

Location calculator

1.0.0 2024-01-17 16:26 UTC

This package is auto-updated.

Last update: 2024-12-17 18:32:05 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

This package provides calculations based on longitude and latitude.

Features

It calculates distance between two locations.

It uses the “haversine” formula to calculate the great-circle distance between two points — that is, the shortest distance over the earth’s surface — giving an “as-the-crow-flies” distance between the points.

Formula:

a = sin²( Δφ / 2 ) + cos φ1 × cos φ2 × sin²( Δλ / 2 )
c = 2 × atan2( √a, √( 1 − a ) )
d = R × c

where φ is latitude, λ is longitude, R is the earth’s radius (mean radius = 6371 km).

Requirements

  • PHP 7.2 or higher.

Installation

The package could be installed with composer:

composer require roslov/location

General usage

Validator allows to check data in any format. For example, when data is an object:

use Roslov\Location\LocationCalculator;

$locationCalculator = new LocationCalculator();
$distance = $locationCalculator->getDistance(49.841905, 24.031511, 50.450885, 30.522798);
echo $distance; // 467.32327173219