I am developing an application with the Geotools package and I have one problem:
public function prodcercanos()
{
$cercanos = User::with('municipio')->with('productos')->get();
$coordA = Geotools::coordinate([44.4578589, 2.2951061999999998]);
foreach ($cercanos as $p) {
$coordB = Geotools::coordinate([$p->municipio->latitud,$p->municipio->longitud]);
$distance = Geotools::distance()->setFrom($coordA)->setTo($coordB);
echo $p->id;
echo "<p>";
echo $distance->in('km')->haversine();
echo "</p>";
}
}
This code returns the distance between points in the foreach
but I need to sort the foreach
with the distance and I don't know how could I make this.
Anyone could help me?