×
Namespaces

Variants
Actions

Retrieving location information

From Nokia Developer Wiki
Jump to: navigation, search

These classes demonstrates how to obtain information related to the current location of the mobile device.

Article Metadata
Article
Created: Den123 (21 Apr 2008)
Last edited: hamishwillee (07 Feb 2012)

Headers required:

#include <lbs.h>
#include <lbspositioninfo.h>
#include <lbssatellite.h>

Link against:

LIBRARY  lbs.lib

Classes:

// helper class-observer
class MPositionReaderObserver
{
public:
// if successful reading
virtual void ReadingComplete( TPositionInfoBase& aPosInfo ) = 0;
// if error occurred
virtual void ReadingError( TInt aErrorNo ) = 0;
};
 
// location info reader
class CPositionReader : public CActive
{
public:
static CPositionReader* NewL( MPositionReaderObserver* anObserver );
~CPositionReader();
 
// requests for reading
void ReadPosInfo();
void ReadCourseInfo();
void ReadSatelliteInfo();
 
// data access
const TPositionInfo& PosInfo();
const TPositionCourseInfo& CourseInfo();
const TPositionSatelliteInfo& SatelliteInfo();
 
public: // from CActive
void RunL();
void DoCancel();
 
private:
CPositionReader();
void ConstructL( MPositionReaderObserver* anObserver );
void ReadInternal( TPositionInfoBase& anInfo );
 
private:
MPositionReaderObserver* iObserver;
 
// used to make the primary connection to the location server
RPositionServer iServer;
// used to create a sub-session with the server
RPositioner iPositioner;
 
TPositionInfoBase* iCurInfo; // points to the current info
TPositionInfo iPositionInfo; // location info
TPositionCourseInfo iPositionCourseInfo; // course info
TPositionSatelliteInfo iPositionSatelliteInfo; // satellites info
};

The body:

// factory
CPositionReader* CPositionReader :: NewL( MPositionReaderObserver* anObserver )
{
CPositionReader* self = new (ELeave) CPositionReader();
CleanupStack :: PushL( self );
self->ConstructL( anObserver );
CleanupStack :: Pop( self );
return self;
}
 
// c++ destructor
CPositionReader :: ~CPositionReader()
{
Cancel();
iPositioner.Close();
iServer.Close();
}
 
void CPositionReader :: ReadPosInfo()
{
ReadInternal( iPositionInfo );
}
const TPositionInfo& CPositionReader :: PosInfo()
{
return iPositionInfo;
}
 
void CPositionReader :: ReadCourseInfo()
{
ReadInternal( iPositionCourseInfo );
}
const TPositionCourseInfo& CPositionReader :: CourseInfo()
{
return iPositionCourseInfo;
}
 
void CPositionReader :: ReadSatelliteInfo()
{
ReadInternal( iPositionSatelliteInfo );
}
const TPositionSatelliteInfo& CPositionReader :: SatelliteInfo()
{
return iPositionSatelliteInfo;
}
 
void CPositionReader :: RunL()
{
if( iStatus == KErrNone )
{
// location retrieved. Operation finished.
iObserver->ReadingComplete( *iCurInfo );
}
else
// error occurred
iObserver->ReadingError( iStatus.Int() );
}
 
void CPositionReader :: DoCancel()
{
// cancel update notify
iPositioner.CancelRequest( EPositionerNotifyPositionUpdate );
}
 
CPositionReader :: CPositionReader():
CActive( CActive :: EPriorityStandard )
{
CActiveScheduler :: Add( this );
}
 
void CPositionReader :: ConstructL( MPositionReaderObserver* anObserver )
{
iObserver = anObserver;
 
// connect to the location server
User :: LeaveIfError( iServer.Connect() );
 
// open the default positioner
User :: LeaveIfError( iPositioner.Open( iServer ) );
 
_LIT( KAppName, "YourAppName" ); // define your application name
User::LeaveIfError( iPositioner.SetRequestor(
CRequestor :: ERequestorService,
CRequestor :: EFormatApplication,
KAppName ) );
 
// set maximum allowed time for a location request
const TInt KTimeout = 50000000; // 50 sec
TTimeIntervalMicroSeconds timeOut( KTimeout );
TPositionUpdateOptions updateOptions;
updateOptions.SetUpdateTimeOut( timeOut );
User::LeaveIfError( iPositioner.SetUpdateOptions( updateOptions ) );
}
 
 
void CPositionReader :: ReadInternal( TPositionInfoBase& anInfo )
{
iCurInfo = &anInfo;
 
// prepare active object
Cancel();
iStatus = KRequestPending;
 
// issuer request
iPositioner.NotifyPositionUpdate( anInfo, iStatus );
SetActive();
}

How to use:

  • implement interface MPositionReaderObserver in your class
  • include CPositionReader* iReader as a class member
  • activate request:
iReader = CPositionReader :: NewL( this ); // new reader
iReader->ReadSatelliteInfo(); // request satellites info
  • process results of reading:
void YourClass :: ReadingComplete( TPositionInfoBase& aPosInfo )
{
TPositionSatelliteInfo& info = ( TPositionSatelliteInfo& )aPosInfo;
TInt iSatellitesInView = info.NumSatellitesInView(); // count of satellites in view
iSatellitesUsed = info.NumSatellitesUsed(); // count of used satellites
...
}

Related Links:

This page was last modified on 7 February 2012, at 09:02.
34 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.

×