How to obtain and save current location
Article Metadata
Class CPositionReader allows to obtain current location. The following code snippet demonstrates, how to obtain location and save it as landmark into default landmark database.
AppUi header:
class CYourAppUi : public CAknAppUi,
public MPositionReaderObserver
{
...
public: // from MPositionReaderObserver
void ReadingComplete( TPositionInfoBase& aPosInfo );
void ReadingError( TInt aErrorNo );
...
private:
CPositionReader* iReader;
};
Initialization and deletion of iReader:
void CYourAppUi :: ConstructL()
{
...
iReader = CPositionReader :: NewL( this ); // new reader
}
CYourAppUi :: ~CYourAppUi()
{
...
delete iReader;
iReader = NULL;
}
You need to activate request for obtaining current location.
iReader->ReadPosInfo();
You can save current location after finishing of the request:
void CYourAppUi :: ReadingComplete( TPositionInfoBase& aPosInfo )
{
// reading current position info
TPositionInfo& info = ( TPositionInfo& )aPosInfo;
TPosition pos;
info.GetPosition( pos );
// open default landmark database
CPosLandmarkDatabase* db = NULL;
TRAPD( err, db = CPosLandmarkDatabase :: OpenL() );
if( err == KErrNone )
{
// create new landmark
CPosLandmark* lm = NULL;
TRAP( err, lm = CPosLandmark :: NewL() );
if( err == KErrNone )
{
// save position info
TRAP( err, lm->SetPositionL( pos ) );
if( err == KErrNone )
{
// fill landmark data
_LIT( KLmName, "Landmark Name" );
lm->SetLandmarkNameL( KLmName );
_LIT( KLmDescription, "Landmark Description" );
lm->SetLandmarkDescriptionL( KLmDescription );
_LIT( KLmCountry, "Landmark Country" );
lm->SetPositionFieldL( EPositionFieldCountry, KLmCountry );
_LIT( KLmCity, "Landmark City" );
lm->SetPositionFieldL( EPositionFieldCity, KLmCity);
_LIT( KLmStreet, "Landmark Street Name and Building Number" );
lm->SetPositionFieldL( EPositionFieldStreet, KLmStreet );
// save landmark
db->AddLandmarkL( *lm );
}
delete lm;
}
delete db;
}
}
Related Links:
- Landmarks/web client example using Carbide.c++ and UI designer
- How to use Landmarks API
- How to select and show a landmark
- How to compact local landmark databases
- How to export landmarks from database to file
- How to import landmarks from file to database
- Execution of landmark operations
- Retrieving location information
- How to manage landmark categories

