//holds the zipcode $templatitude = $dbarray1[1]; //holds the latitude $templongitude = $dbarray1[2]; //holds the longitude //$provarray = array(); //will hold all the provider keys in the order in which they were processed //$distarray = array(); //will hold all the distances in the order in which they were processed $mergearray = array(); //will hold providerkeys+distances in the order in which they were provessed //foreach($dbarray2 as $dbarray[$i]) while ($dbarray[$i] = mysql_fetch_array($result2)) { //echo $templatitude.", ".$templongitude."---".$dbarray[$i]['Latitude'].", ".$dbarray[$i]['Longitude']."<br>"; //$provarray= $dbarray[$i]['provider_key']; //$distarray= straight_line_distance($templatitude,$templongitude,$dbarray[$i]['Latitude'],$dbarray[$i]['Longitude']); if ($dbarray[$i][1] != 0) { $mergearray[] = $dbarray[$i][0] . haversine($templatitude, $templongitude, $dbarray[$i][1], $dbarray[$i][2]); } } //Sort top results based on the selection by the user. //$testVar = testFunc(); //echo $testVar; $mergearray = quickSortRecursive($mergearray); $no_of_results = (int) $no_of_results; $no_of_results = $no_of_results + 5; // Array to hold distances $dbarray_dist; //calc geocoded distance between top results //$result = array(); for ($i = 0; $i < $no_of_results; $i++) { //If distance is greater than 250 do not include the facility . 200 is used here as all distances are straight line distances and not driving. if (substr($mergearray[$i], 10) < 200) {
function geolookupByLatLon($lat, $lon) { $db = mysql_connect("localhost", "havard", "zo1nk"); mysql_select_db("nrktilaatapaa"); //$sql = "SELECT * FROM geolookup WHERE (hp('intval', lat) = 60 AND php('intval', lon) = 7)"; $sql = "SELECT * FROM geolookup WHERE FLOOR(lat) = " . intval($lat) . " AND FLOOR(lon) = " . intval($lon); $result = mysql_query($sql); $closest = false; $dist = -1; while ($r = mysql_fetch_assoc($result)) { #$rdist = haversine($lat, $r["LAT"], $lon, $r["LON"]); $rdist = haversine($lat, $lon, $r["LAT"], $r["LON"]); #printf("distance is %s to %s\n", $rdist, $r["MUNICIP"]); if ($rdist < $dist || $dist == -1) { $dist = $rdist; $closest = $r; } } #var_dump($closest, $dist); $c = intval(substr($closest["MUNICIP"], 0, 2)); return array("municipalitycode" => $closest["MUNICIP"], "lat" => $lat, "lon" => $lon, "county" => $c, "countyname" => countyNameByCode($c)); }
/** * Uses the haversine formula to calculate the distance between 2 points. * * @param Point $point1 * @param Point $point2 * * @return float distance in radians */ public static function haversine(Point $point1, Point $point2) { if (function_exists('haversine') && self::$useSpatialExtension) { $from = $point1->jsonSerialize(); $to = $point2->jsonSerialize(); $radDistance = haversine($from, $to, 1); } else { $lat1 = $point1->latitudeToRad(); $lon1 = $point1->longitudeToRad(); $lat2 = $point2->latitudeToRad(); $lon2 = $point2->longitudeToRad(); $distanceLat = $lat1 - $lat2; $distanceLong = $lon1 - $lon2; $radDistance = sin($distanceLat / 2) * sin($distanceLat / 2) + cos($lat1) * cos($lat2) * sin($distanceLong / 2) * sin($distanceLong / 2); $radDistance = 2 * atan2(sqrt($radDistance), sqrt(1 - $radDistance)); } return $radDistance; }