Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Obtaining current device position

From Wiki
Jump to: navigation, search
Article Metadata
Created: Den123 (23 Apr 2008)
Last edited: hamishwillee (25 Jul 2012)

The following code snippet demonstrates how to use class CPositionReader for obtaining current device position. This snippet also demonstrates, how to find the nearest landmark according to the current position.

Your class should implement interface CPositionReaderObserver and include instance of CPositionReader as a memeber.

It is necessary to include the following additional headers:

  • epos_landmarks.h
  • epos_cposlmitemiterator.h
  • epos_cposlandmarkdatabase.h

Start async. request:

iReader = CPositionReader :: NewL( this );	

You should add the following code in the method ReadingComplete() for reading current location and finding the nearest landmark:

// read result device position
TPositionInfo& info = ( TPositionInfo& )aPosInfo;
TPosition position;
info.GetPosition( position );
// open default landmark DB
CPosLandmarkDatabase* db = CPosLandmarkDatabase :: OpenL();
CleanupStack :: PushL( db );
// create iterator
CPosLmItemIterator* iter = db->LandmarkIteratorL();
CleanupStack :: PushL( iter );
// distance between device position and current landmark
TReal32 distance = KMaxTReal32;
TPosLmItemId itemId = iter->NextL(), // current landmark ID
resultItemId = KPosLmNullItemId; // nearest landmark ID
while( itemId != KPosLmNullItemId )
// read current landmark
CPosLandmark* lm = db->ReadLandmarkLC( itemId );
// get current landmark position
TPosition lmPosition;
if( lm->GetPosition( lmPosition ) == KErrNone )
TReal32 d;
// check distance
if( position.Distance( lmPosition, d ) == KErrNone && d < distance )
resultItemId = itemId;
distance = d;
// read next landmark ID
itemId = iter->NextL();
CleanupStack :: PopAndDestroy(); // lm
// landmark was found
if( resultItemId != KPosLmNullItemId )
// read landmark from DB
CPosLandmark* resultLm = db->ReadLandmarkLC( resultItemId );
// read landmark name
TPtrC lmName;
resultLm->GetLandmarkName( lmName );
// show nearest landmark name
CAknInformationNote* informationNote;
informationNote = new ( ELeave ) CAknInformationNote;
informationNote->ExecuteLD( lmName );
CleanupStack :: PopAndDestroy(); // resultLm
CleanupStack :: PopAndDestroy( 2 ); // iter db
This page was last modified on 25 July 2012, at 05:44.
58 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.