Пример #1
0
/**
 *
 * @param GuifiAPI $gapi GuifiAPI object
 *
 * @param mixed[] $parameters
 *
 * @return
 */
function guifi_api_zone_nearest($gapi, $parameters)
{
    if (!guifi_api_check_fields($gapi, array('lat', 'lon'), $parameters)) {
        return FALSE;
    }
    $candidates = guifi_zone_get_nearest_candidates($parameters['lat'], $parameters['lon']);
    foreach ($candidates as &$candidate) {
        $candidate['zone_id'] = $candidate['id'];
        unset($candidate['min_lon'], $candidate['max_lon'], $candidate['min_lat'], $candidate['max_lat'], $candidate['d'], $candidate['id']);
    }
    $nearest = guifi_zone_get_nearest($parameters['lat'], $parameters['lon'], $candidates);
    unset($nearest['d']);
    $gapi->addResponseField('candidates', $candidates);
    $gapi->addResponseField('nearest', $nearest);
    return TRUE;
}
Пример #2
0
/**
 * Gets nearest zone of a selected point
 * @param $lat Latitude of the point
 * @param $lon Longitude of the point
 * @return mixed[] The best selected zone which can contain a point
 */
function guifi_zone_get_nearest($lat, $lon, $zones = NULL)
{
    if ($zones == NULL) {
        $zones = guifi_zone_get_nearest_candidates($lat, $lon);
    }
    if ($zones) {
        $maxd = 0;
        $oGC = new GeoCalc();
        foreach ($zones as $zone) {
            if (empty($zone['d'])) {
                $d1 = $oGC->EllipsoidDistance($lat, $lon, $zone['max_lat'], $zone['max_lon']);
                $d2 = $oGC->EllipsoidDistance($lat, $lon, $zone['min_lat'], $zone['min_lon']);
                $zone['d'] = sqrt($d1 * $d1 + $d2 * $d2);
            }
            if (empty($maxd) || $zone['d'] < $maxd) {
                $maxd = $zone['d'];
                $candidate_zone = $zone;
            }
        }
        return $candidate_zone;
    } else {
        return FALSE;
    }
}