Пример #1
0
function findNearestStations2($location)
{
    global $markers;
    // the list of stations to return
    $stations = array();
    $maxDist = 0;
    foreach ($markers as $marker) {
        $currentStation = new Station2($marker, distance2Loc($location, $marker));
        if (count($stations) == 3) {
            $maxDist = max($stations[0]->getDistance(), $stations[1]->getDistance(), $stations[2]->getDistance());
        }
        if (count($stations) < 3) {
            $stations[] = $currentStation;
            if (count($stations) < 3) {
                continue;
            }
        }
        if ($maxDist > $currentStation->getDistance()) {
            //Find station with maxDist
            $i = 0;
            for (; $i < count($stations); $i++) {
                if ($stations[$i]->getDistance() == $maxDist) {
                    break;
                }
            }
            //Replace station with maxDist with currentStation
            $stations[$i] = $currentStation;
        }
    }
    // this array should be the three closest stations
    return $stations;
}
Пример #2
0
/**
    Builds the marker from data in the database and returns it.
    @param id<String> the marker id
    @return <Station2> a Station2 object
*/
function retrieveStation($id)
{
    global $stations;
    $debug = false;
    outputDebug("algorithm.retrieveStation({$id})", $debug);
    $marker = $stations[$id];
    if ($marker != null) {
        outputDebug("{$id} retrieved from cache.", $debug);
        return $marker;
    }
    $query = "SELECT * FROM markers WHERE id='{$id}'";
    outputDebug($query, $debug);
    $result = mysql_query($query) or die("Unable to retrieve marker of {$id} from the database.");
    if (count($result) > 1) {
        // error! there should only be one result
        echo "algorithm.getMarker() returned more than one result.<br/>\n";
    }
    $query_array = mysql_fetch_array($result, MYSQL_BOTH);
    $marker = new Station2($query_array['lat'], $query_array['lng'], $query_array['id'], $query_array['name']);
    $query = "SELECT * FROM connections WHERE marker_id='{$id}'";
    outputDebug($query, $debug);
    $result = mysql_query($query) or die("Unable to retrieve connections for {$id} from the database.");
    while ($c_array = mysql_fetch_array($result, MYSQL_BOTH)) {
        $connection = new Connection($c_array['id'], $c_array['type'], $c_array['start'], $c_array['end'], $c_array['day'], $c_array['duration']);
        $line = $marker->getLine($c_array['line']);
        if ($line == null) {
            // line doesn't exist yet, so create it
            $line_query = "SELECT * FROM trains WHERE name='" . $c_array['line'] . "'";
            outputDebug($line_query, $debug);
            $line_result = mysql_query($line_query) or die("Unable to retrieve line information for " . $c_array['line'] . " from the database.");
            if (count($line_result) > 1) {
                echo "algorithm.getMarker() returned more than one result when retrieving line " . $c_array['line'];
            }
            $line_array = mysql_fetch_array($line_result, MYSQL_BOTH);
            $line = new Line($line_array['img'], $line_array['url'], $line_array['name']);
            // add the line to the marker
            $marker->addLine($line);
        }
        // add the connection to the line
        $line->addConnection($connection);
    }
    // connection loop
    // done, return marker
    if ($debug) {
        $marker->printInfo();
    }
    $stations[$id] = $marker;
    outputDebug("{$id} retrieved from database.", $debug);
    return $marker;
}