Condividi tramite


Metodo ILocationEvents::OnLocationChanged (locationapi.h)

[L'API Percorso Win32 è disponibile per l'uso nei sistemi operativi specificati nella sezione Requisiti. È possibile che in versioni successive sia stata modificata o non sia più disponibile. Usare invece l'API Windows.Devices.Geolocation . ]

Chiamato quando è disponibile un nuovo report della posizione.

Sintassi

HRESULT OnLocationChanged(
  [in] REFIID          reportType,
  [in] ILocationReport *pLocationReport
);

Parametri

[in] reportType

REFIID contenente l'ID interfaccia del tipo di report contenuto in pLocationReport.

[in] pLocationReport

Puntatore all'istanza di ILocationReport contenente il nuovo report del percorso.

Valore restituito

Se questo metodo ha esito positivo, restituisce S_OK. In caso contrario, restituisce un codice di errore HRESULT .

Commenti

ILocationReport è l'interfaccia di base di tipi di report di posizione specifici. L'interfaccia effettiva ricevuta dal chiamante per pLocationReport corrisponderà al tipo specificato da reportType.

Se l'applicazione chiama OnLocationChanged come risultato del suo primo utilizzo della posizione, la chiamata potrebbe causare la visualizzazione di una notifica nella barra delle applicazioni e causare l'accesso di un evento Attività percorso in Visualizzatore eventi.

Nota Un'applicazione non riceve l'evento di modifica della posizione previsto da OnLocationChanged se entrambe le condizioni seguenti sono vere. Prima di tutto, l'applicazione viene eseguita come servizio nel contesto dell'account utente LOCALSERVICE, SYSTEM o NETWORKSERVICE. In secondo luogo, l'evento di modifica della posizione comporta la modifica della posizione predefinita, manualmente quando l'utente seleziona Percorso predefinito in Pannello di controllo o a livello di codice quando un'applicazione chiama IDefaultLocation::SetReport.
 

Esempio

L'implementazione di esempio seguente di OnLocationChanged gestisce l'evento di modifica della posizione per un report di latitudine/longitudine. Questa implementazione stampa le informazioni seguenti su un evento di modifica della posizione di latitudine/longitudine: timestamp, ID sensore, latitudine, il raggio di errore, l'altitudine e l'errore di altitudine.

// This is called when there is a new location report
STDMETHODIMP CLocationEvents::OnLocationChanged(REFIID reportType, ILocationReport* pLocationReport)
{
    // If the report type is a Latitude/Longitude report (as opposed to IID_ICivicAddressReport or another type)
    if (IID_ILatLongReport == reportType)
    {
        CComPtr<ILatLongReport> spLatLongReport;

        // Get the ILatLongReport interface from ILocationReport
        if ((SUCCEEDED(pLocationReport->QueryInterface(IID_PPV_ARGS(&spLatLongReport)))) && (NULL != spLatLongReport.p))
        {
            // Print the Report Type GUID
            wchar_t szGUID[64];
            wprintf(L"\nReportType: %s", GUIDToString(IID_ILatLongReport, szGUID, ARRAYSIZE(szGUID)));

            // Print the Timestamp and the time since the last report
            SYSTEMTIME systemTime;
            if (SUCCEEDED(spLatLongReport->GetTimestamp(&systemTime)))
            {
                // Compute the number of 100ns units that difference between the current report's time and the previous report's time.
                ULONGLONG currentTime = 0, diffTime = 0;
                if (TRUE == SystemTimeToFileTime(&systemTime, (FILETIME*)&currentTime))
                {
                    diffTime = (currentTime > m_previousTime) ? (currentTime - m_previousTime) : 0;
                }

                wprintf(L"\nTimestamp: YY:%d, MM:%d, DD:%d, HH:%d, MM:%d, SS:%d, MS:%d [%I64d]\n",
                    systemTime.wYear,
                    systemTime.wMonth,
                    systemTime.wDay,
                    systemTime.wHour,
                    systemTime.wMinute,
                    systemTime.wSecond,
                    systemTime.wMilliseconds,
                    diffTime / 10000); // Display in milliseconds

                m_previousTime = currentTime; // Set the previous time to the current time for the next report.
            }

            // Print the Sensor ID GUID
            GUID sensorID = {0};
            if (SUCCEEDED(spLatLongReport->GetSensorID(&sensorID)))
            {
                wchar_t szGUID[64];
                wprintf(L"SensorID: %s\n", GUIDToString(sensorID, szGUID, ARRAYSIZE(szGUID)));
            }

            DOUBLE latitude = 0, longitude = 0, altitude = 0, errorRadius = 0, altitudeError = 0;

            // Print the Latitude
            if (SUCCEEDED(spLatLongReport->GetLatitude(&latitude)))
            {
                wprintf(L"Latitude: %f\n", latitude);
            }

            // Print the Longitude
            if (SUCCEEDED(spLatLongReport->GetLongitude(&longitude)))
            {
                wprintf(L"Longitude: %f\n", longitude);
            }

            // Print the Altitude
            if (SUCCEEDED(spLatLongReport->GetAltitude(&altitude)))
            {
                wprintf(L"Altitude: %f\n", altitude);
            }
            else
            {
                // Altitude is optional and may not be available
                wprintf(L"Altitude: Not available.\n");
            }

            // Print the Error Radius
            if (SUCCEEDED(spLatLongReport->GetErrorRadius(&errorRadius)))
            {
                wprintf(L"Error Radius: %f\n", errorRadius);
            }

            // Print the Altitude Error
            if (SUCCEEDED(spLatLongReport->GetAltitudeError(&altitudeError)))
            {
                wprintf(L"Altitude Error: %f\n", altitudeError);
            }
            else
            {
                // Altitude Error is optional and may not be available
                wprintf(L"Altitude Error: Not available.\n");
            }
        }
    }

    return S_OK;
}

Requisiti

Requisito Valore
Client minimo supportato Windows 7 [solo app desktop],Windows 7
Server minimo supportato Nessuno supportato
Piattaforma di destinazione Windows
Intestazione locationapi.h
DLL LocationAPI.dll