Starting from Phpgeo itself, as mentioned in the question:
Vincenty’s formula
<?php
use Location\Coordinate;
use Location\Distance\Vincenty;
$coordenada1 = new Coordinate( -23.575, -46.658 );
$coordenada2 = new Coordinate( -21.279, -44.297 );
$calculadora = new Vincenty();
echo $calculadora->getDistance( $coordenada1, $coordenada2 );
?>
To haversine formula is much faster to calculate, but less precise.
<?php
use Location\Coordinate;
use Location\Distance\Haversine;
$coordenada1 = new Coordinate( -23.575, -46.658 );
$coordenada2 = new Coordinate( -21.279, -44.297 );
$calculadora = new Haversine();
echo $calculadora->getDistance( $coordenada1, $coordenada2 );
?>
Github - Phpgeo
Calculating the distance of the origin to several points, and ordering:
To sort the results, you can start from the reference point, and in a loop store the calculated distances in an array, and then sort them:
<?php
$origem = new Coordinate( -22.155, -45.158 );
// Lembre-se de inicializar as arrays de acordo com seu código
$nome[1] = 'Ponto1';
$coordenada[1] = new Coordinate( -23.575, -46.658 );
$nome[2] = 'Ponto2';
$coordenada[2] = new Coordinate( -21.279, -44.297 );
$nome[3] = 'Ponto3';
$coordenada[3] = new Coordinate( -21.172, -44.017 );
... etc ...
// Vamos calcular a distância de todos para a $origem:
$calculadora = new Haversine();
$count = count( $coordenada );
for ($i = 0; $i < $count; $i++) {
$distancia[i] = calculadora->getDistance( $origem, $coordenada[i] );
}
// Agora ordenamos os pontos de acordo com a $distancia:
array_multisort( $distancia, $coordenada, $nome );
?>
You are doing a select across BD and then bringing all the results, to filter in PHP which one is closest?
– Marcelo Aymone