/** * Update user's location */ public function post_location(array $args) { if (isset($_POST['latitude']) && isset($_POST['longitude'])) { $spot = new midgardmvc_helper_location_spot((double) $_POST['latitude'], (double) $_POST['longitude']); if (isset($_POST['text'])) { // User has provided a textual location $spot->text = $_POST['text']; } else { try { // Get textual location by reverse geocoding $geocoder = new midgardmvc_helper_location_geocoder_geonames(); $city = $geocoder->reverse_geocode($spot); if ($city->text) { $spot->text = $city->text; } } catch (Exception $e) { // Ignore silently } } } elseif (isset($_POST['text'])) { $spot = new midgardmvc_helper_location_spot($_POST['text']); } else { throw new InvalidArgumentException("Expected latitude and longitude, or text not found"); } if (isset($_POST['accuracy'])) { // W3C accuracy is in meters, convert to our approximates if ($_POST['accuracy'] < 30) { // Exact enough $spot->accuracy = 10; } elseif ($_POST['accuracy'] < 400) { // Postal code area $spot->accuracy = 20; } elseif ($_POST['accuracy'] < 5000) { // City $spot->accuracy = 30; } else { // Fall back to "state level" $spot->accuracy = 50; } } $spot->source = 'browser'; if (!midgardmvc_helper_location_user::set_location($spot)) { throw new midgardmvc_exception_httperror("Failed to store location"); } midgardmvc_core::get_instance()->log("postlocation", "Location stored", 'debug'); $this->get_location($args); }
public function test_helsinki_reverse() { $museokatu = new midgardmvc_helper_location_spot(60.175416157517, 24.919127225876); $geocoder = new midgardmvc_helper_location_geocoder_geonames(); $spot = $geocoder->reverse_geocode($museokatu); // Check that we got the correct type $this->assertTrue(is_a($spot, 'midgardmvc_helper_location_spot')); // Check that the type is near Helsinki $this->assertEquals((int) round($spot->latitude), 60); $this->assertEquals((int) round($spot->longitude), 25); // Check that we got city and country $this->assertEquals($spot->country, 'FI'); $this->assertEquals($spot->city, 'Etu-Töölö'); // Check that accuracy is correctly set to "city" $this->assertEquals($spot->accuracy, 30); // Check that source is correct $this->assertEquals($spot->source, 'geonames'); }