Location Acquisition API的使用总结
1. Connect to Location Server: RPositionServer::Connect().
2. Create a sub-session with Location Server to retrieve location updates: RPositioner::Open().
3. Inform Location Server who is requesting the location information: RPositioner::SetRequestor().
4. Set the update options (e.g. to obtain periodic updates): RPositioner::SetUpdateOptions().
5. Request to be informed (asynchronously) of position updates: RPositioner::NotifyPositionUpdate(). As a standard, NotifyPositionUpdate() takes the parameters TPositionInfo and a TRequestStatus. After the request has been completed, the location information is stored in TPositionInfo and TRequestStatus contains the result of the request.
6. Extract the current position: TPositionInfo::GetPosition().
7. Re-issue the request to obtain the next position update: RPositioner::NotifyPositionUpdate().
8. Cancel any outstanding request(s): RPositioner::CancelRequest() and PositionServer::CancelRequest().
9. Close the sub-session: RPositioner::Close().
10. Close the Location Server session: RPositionServer::Close().
下面是一个简单的例子说明:
Code:
#include <lbs.h>
...
// Init Connection
RPositionServer server;
RPositioner positioner;
User::LeaveIfError(server.Connect());
CleanupClosePushL(server);
User::LeaveIfError(positioner.Open(server)); // use default positioning module
CleanupClosePushL(positioner);
// Specify Requestors
_LIT(KCntPhone, "+358501234567");
_LIT(KSrvName, "MyService");
RRequestorStack stack;
CRequestor* contact = CRequestor::NewLC(
CRequestor::ERequestorContact,
CRequestor::EFormatTelephone,
KCntPhone);
stack.Append(contact);
CRequestor* service = CRequestor::NewLC(
CRequestor::ERequestorService,
CRequestor::EFormatApplication,
KSrvName);
stack.Append(service);
User::LeaveIfError(positioner.SetRequestor(stack));
//User::LeaveIfError(positioner.SetRequestor(CRequestor::ERequestorService ,
// CRequestor::EFormatApplication ,
// KSrvName)); //Alternative usage
// Issue a Location Request
TRequestStatus status;
TPositionInfo posInfo;
positioner.NotifyPositionUpdate(posInfo, status);
User::WaitForRequest(status);
User::LeaveIfError(status.Int());
// Analyze Results
TPosition position;
posInfo.GetPosition(position);
// Issue a new Location Request
positioner.NotifyPositionUpdate(posInfo, status);
User::WaitForRequest(status);
User::LeaveIfError(status.Int());
// Cleanup
stack.Reset();
CleanupStack::PopAndDestroy(service);
CleanupStack::PopAndDestroy(contact);
CleanupStack::PopAndDestroy(&positioner); // this will call Close() method
CleanupStack::PopAndDestroy(&server); // this will call Close() method