Friday, 15 February 2013

php - Finding the nearest neighbor Algorithm using google map coordinates -



php - Finding the nearest neighbor Algorithm using google map coordinates -

please help me find algorithm finds nearest neighbour coordinates (latitude/longitude) implemented using php

for example. have 1 client , 2 nodes node 1 = 32.9697, -96.8032 , node 2=42.9697, -97.80322

each node has own given coordinates (place on map).

the client send latitude , longitude coordinates scheme , scheme find out if coordinates receives client near node 1 or node 2

please excuse grammar. hoping kind response. give thanks in advance

if have limited number of possible targets (as guess question) can utilize next function (copied here) , iterate on targets find closest one.

function distance($lat1, $lng1, $lat2, $lng2, $miles = false) { $pi80 = m_pi / 180; $lat1 *= $pi80; $lng1 *= $pi80; $lat2 *= $pi80; $lng2 *= $pi80; $r = 6372.797; // mean radius of earth in km $dlat = $lat2 - $lat1; $dlng = $lng2 - $lng1; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $km = $r * $c; homecoming ($miles ? ($km * 0.621371192) : $km); }

if have many possible locations (>=10^4) should organise info points in construction have evaluate fraction of them. i'd suggest quadtree although not work poles datum-border. i'm sure you'll find improve solutions if needed such case (which assume not require).

php algorithm coordinates

No comments:

Post a Comment