public function ajax_getSuggestionByCoordinates(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $lat = $params->getFloat('lat'); $lon = $params->getFloat('lon'); $location = CM_Model_Location::findByCoordinates($lat, $lon); $location = $this->_squashLocationInConstraints($location); if (!$location) { throw new CM_Exception('Cannot find a location by coordinates `' . $lat . '` / `' . $lon . '`.'); } return $this->getSuggestion($location, $response->getRender()); }
public function testFindByCoordinates() { CM_Db_Db::insert('cm_model_location_city', array('stateId' => self::$_fields[CM_Model_Location::LEVEL_STATE]['id'], 'countryId' => self::$_fields[CM_Model_Location::LEVEL_COUNTRY]['id'], 'name' => 'test', 'lat' => 20, 'lon' => 20)); $idExpected1 = CM_Db_Db::insert('cm_model_location_city', array('stateId' => self::$_fields[CM_Model_Location::LEVEL_STATE]['id'], 'countryId' => self::$_fields[CM_Model_Location::LEVEL_COUNTRY]['id'], 'name' => 'test', 'lat' => 20.1, 'lon' => 20.2)); $idExpected2 = CM_Db_Db::insert('cm_model_location_city', array('stateId' => self::$_fields[CM_Model_Location::LEVEL_STATE]['id'], 'countryId' => self::$_fields[CM_Model_Location::LEVEL_COUNTRY]['id'], 'name' => 'Waite Park', 'lat' => 45.53, 'lon' => -94.233)); $locationExpected1 = new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $idExpected1); $locationExpected2 = new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $idExpected2); CM_Model_Location::createAggregation(); $this->assertNull(CM_Model_Location::findByCoordinates(100, 100)); $this->assertEquals($locationExpected1, CM_Model_Location::findByCoordinates(20, 20.3)); $this->assertEquals($locationExpected2, CM_Model_Location::findByCoordinates(45.552222998855, -94.21451630079601)); }