public static function get_location($when = null) { if (midcom::get('auth')->user) { // Get from user's location log return org_routamc_positioning_user::get_location_for_person(midcom::get('auth')->user->get_storage(), $when); } // Get from session $session = new midcom_services_session(); if (!$session->exists('org_routamc_positioning_user_location')) { return null; } return $session->get('org_routamc_positioning_user_location'); }
<?php midcom::get('auth')->require_valid_user(); // Read location from session or user's location log $user_location = org_routamc_positioning_user::get_location(); if (is_null($user_location)) { // No location found, try to geocode based on user IP $geocoder = org_routamc_positioning_geocoder::create('geoplugin'); $location_parameters = array('ip' => $_SERVER['REMOTE_ADDR']); $user_location = $geocoder->geocode($location_parameters); if (!is_null($user_location)) { // Store geocoded location to session or user's location log org_routamc_positioning_user::set_location($user_location); } } if (!is_null($user_location)) { echo sprintf('You\'re in %s, %s', $user_location['latitude'], $user_location['longitude']); // Will print "You're in 60.2345, 25.00456" } else { if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') { echo "You're here"; } else { echo "No location found"; } }
<?php midcom::get()->header('Content-type: application/json'); if (isset($_POST['latitude']) && isset($_POST['longitude'])) { // Updating user's location $location_array = array('latitude' => (double) $_POST['latitude'], 'longitude' => (double) $_POST['longitude']); if (isset($_POST['accuracy'])) { // W3C accuracy is in meters, convert to our approximates if ($_POST['accuracy'] < 30) { // Exact enough $location_array['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; } elseif ($_POST['accuracy'] < 400) { $location_array['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_STREET; } elseif ($_POST['accuracy'] < 5000) { $location_array['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; } else { // Fall back to "state level" $location_array['accuracy'] = 50; } } $location_array['source'] = 'browser'; org_routamc_positioning_user::set_location($location_array); } echo json_encode(org_routamc_positioning_user::get_location());