This page was last modified 04:45, 22 April 2008.
Retrieving location information
From Forum Nokia Wiki
These classes demonstrates how to obtain information related to the current location of the mobile device.
It is necessary to include the following headers:
- lbs.h
- lbspositioninfo.h
- lbssatellite.h
And link against:
- 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 Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Avoiding the Location capability | storsjo | Symbian Signing, Certification and Security | 12 | 2008-03-18 16:18 |
| Retrieving Wireless LAN Access Points | satyarajasekhar | Location Based Services and Navigation | 3 | 2007-12-03 11:43 |
| Location API (JSR 179) without GPS.. | d33 | Mobile Java General | 19 | 2008-05-28 04:59 |
| ID3, atoms, metadata & the MMF | Gingah | Symbian Media (Graphics & Sounds) | 1 | 2007-02-08 12:19 |
| how to read location information from SIM card? | bamboowind | General Symbian C++ | 0 | 2004-10-28 06:44 |
