2017-10-03 6 views
-3

ExempleJ'ai coordonnées piscine avec x, y combniation je dois correspondre correspondance la plus proche de x données, y

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

Je dois correspondre x, y somme non de x et y, mais plus proche de x et y . pas le plus proche de (x + y) ou ((x1-x) + (y1-y)) les deux sont faux J'ai besoin de correspondance parfaite.

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); 
+1

Nous sommes toujours heureux d'aider et de soutenir les nouveaux codeurs mais *** vous devez vous aider en premier. : -) *** Après [** faire plus de recherche **] (https://meta.stackoverflow.com/q/261592/1011527) si vous avez un problème ** poster ce que vous avez essayé ** avec un ** explication claire de ce qui ne fonctionne pas ** et fournir [un exemple minimal, complet et vérifiable] (http://stackoverflow.com/help/mcve). Lisez [Comment demander] (http://stackoverflow.com/help/how-to-ask) une bonne question. Assurez-vous de [faire le tour] (http://stackoverflow.com/tour) et lisez [this] (https://meta.stackoverflow.com/q/347937/1011527). –

+0

Une paire de valeurs doit correspondre au tableau de la paire de valeurs. – karimmughal

+0

essayez-vous de résoudre pour l'hypoténuse? – Borik

Répondre

0

Peut-être que cela vous aidera à obtenir sur la bonne voie:

<?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."; 
} 

Sortie:

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

Une autre solution à l'aide 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); 

Sortie:

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

Toujours se tromper répondre – karimmughal

+0

@karimmughal a ajouté le code complet ... – Borik