Share via


Long Running Location Application

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

The following shows a typical application usage of the Location Framework for an application that requires continuous position updates for lat/long. Note error checking has been excluded to increase clarity, though any Location Framework API may fail.

Syntax

// Base handle to Location Framework.  // This will not cause any plugins to be started
HLOCATION hLoc = LocationOpen(LOCATION_FRAMEWORK_VERSION_1,NULL,0);

// Event that Location Framework signals when a new position is
//  generated by a plugin of this particular type
HANDLE hNewReportEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
// Event that Location Framework signals when the underlying
//  state of a plugin that generates this report changes
HANDLE hPluginStateChangeEvent = CreateEvent(NULL,FALSE,FALSE,NULL);

// Indicate to Location Framework that lat/long should be generated,
//  if not already.  This will cause location plugin(s) to be started
LocationRegisterForReport(hLoc, hNewReportEvent, hPluginStateChangeEvent,
                                              LOCATION_LATLONG_GUID,0); 

while (appWishesToGetLocation) {
    // Wait for new report to be signaled
    WaitForSingleObject(hNewReportEvent,maxTimeout);
    if (eventTimedOut) { PossiblyIndicateErrorToUser(); continue; }

    // Retrieve the report 
    LOCATION_REPORT_LATLONG currentPosition;
    DWORD currentPositionSize = sizeof(currentPosition);
    LocationGetReport(hLoc, LOCATION_LATLONG_GUID,10000,
                                    &currentPosition,&currentPositionSize,0);
    PerformActionBasedOnReport(&currentPosition);
}

// No longer need this report type
LocationUnRegisterForReport(hLoc,LOCATION_LATLONG_GUID,0);

// Close down our open handle of location framework
LocationClose(hLoc);  

See Also

Other Resources

Location Framework Samples