×
Namespaces

Variants
Actions

How to obtain and save current location

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: Den123 (22 May 2008)
Last edited: hamishwillee (07 Feb 2012)

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:

This page was last modified on 7 February 2012, at 09:21.
57 page views in the last 30 days.
×