У меня есть пул координат с x, y комбинацией, мне нужно, чтобы найти ближайшее совпадение с заданным x, y

пример

1   8.919280,45.622651
2   8.910296,45.626021
3   8.914084,45.627028
4   8.913913,45.62941

Я должен соответствовать x, y не сумма x и y, а ближайшие x и y.
не самые близкие из (x + y) или ((x1-x) + (y1-y)) оба неверны, мне нужно идеальное совпадение.

8.912680,45.629392

foreach($pair_coordinates as $pair_coordinate)
{
sscanf((string)$pair_coordinate, '%f,%f',$longitude, $latitude);
$data[$move]['lng'] = $longitude;
$data[$move]['lat'] = $latitude;
$data[$move]['longitude'] = ($longitude-$_POST['k_lng']);
$data[$move]['latitude'] = ($latitude-$_POST['k_lat']);

$data[$move]['diff'] = $data[$move]['longitude'] + $data[$move]['latitude'];

$data[$move]['diff'] = abs($data[$move]['diff']);

$sort[$move] = $data[$move]['diff'];
$sort_old[$move] = $data[$move]['diff'];

$data[$move]['zona'] = (string)$placemarker->ExtendedData->Data[2]->value;
$data[$move]['codcom'] = (string)$placemarker->ExtendedData->Data[1]->value;
$data[$move]['markername'] = (string)$placemarker->name;
if($data[$move]['longitude'] < 0 || $data[$move]['latitude'] < 0)
{
unset($sort[$move]);
}$move++;
}asort($sort);

-3

Решение

Может быть, это поможет вам встать на путь:

<?php

$coords = array(
array("lng" => 8.919280, "lat" => 45.622651),
array("lng" => 8.910296, "lat" => 45.626021),
array("lng" => 8.914084, "lat" => 45.627028),
array("lng" => 8.913913, "lat" => 45.62941)
);

$find = array("lng" => 8.912680, "lat" => 45.629392);

$idx = null;
$shortest = null;

foreach($coords as $k => $v)
{
// I know the abs() calls are optional, but this allows for some other logic to be used on the individual distances aswell.
$dLng = abs($find["lng"] - $v["lng"]);
$dLat = abs($find["lat"] - $v["lat"]);

$distance = sqrt($dLng * $dLng + $dLat * $dLat);

if (!$shortest || $distance < $shortest)
{
$shortest = $distance;
$idx = $k;
}
}

if ($idx)
{
echo "Found:\n";
print_r($coords[$idx]);
} else {
echo "Nothing found.";
}

Выход:

Found:
Array
(
[lng] => 8.913913
[lat] => 45.62941
)

Другое решение с использованием array_reduce:

<?php

$coords = array(
array("lng" => 8.919280, "lat" => 45.622651),
array("lng" => 8.910296, "lat" => 45.626021),
array("lng" => 8.914084, "lat" => 45.627028),
array("lng" => 8.913913, "lat" => 45.62941)
);

$find = array("lng" => 8.912680, "lat" => 45.629392);

function distance($a, $b)
{
$dLng = abs($a["lng"] - $b["lng"]);
$dLat = abs($a["lat"] - $b["lat"]);

$distance = sqrt($dLng * $dLng + $dLat * $dLat);

return $distance;
}$nearest = array_reduce($coords, function($carry, $item) use ($find)
{
if (!$carry)
return $item;

$dCarry = distance($find, $carry);
$dItem = distance($find, $item);

return $dCarry < $dItem ? $carry : $item;
}, null);

print_r($nearest);

Выход:

Array
(
[lng] => 8.913913
[lat] => 45.62941
)
0

Другие решения

$pair_coordinates = array(array(10.919280,45.622651),array(8.910296,45.626021),array(8.914084,45.627028), array(8.913913,45.62941 ));

$i = 0;

$x = 8.919380;
$y = 45.623651;

foreach($pair_coordinates as $pair_coordinate)
{$lng = $pair_coordinate[0] - $x;
$lon = $pair_coordinate[1] - $y;

$data[$i] = sqrt(pow($lng,2) + pow($lon,2));

$i++;
}

$m = array_search(min($data), $data);

print_r($pair_coordinates[$m]);
0

По вопросам рекламы [email protected]