Bagikan melalui


Metode ILocationEvents::OnLocationChanged (locationapi.h)

[API Lokasi Win32 tersedia untuk digunakan dalam sistem operasi yang ditentukan di bagian Persyaratan. Ini mungkin diubah atau tidak tersedia dalam versi berikutnya. Sebagai gantinya, gunakan API Windows.Devices.Geolocation . ]

Dipanggil saat laporan lokasi baru tersedia.

Sintaks

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

Parameter

[in] reportType

REFIID yang berisi ID antarmuka jenis laporan yang terkandung dalam pLocationReport.

[in] pLocationReport

Arahkan ke instans ILocationReport yang berisi laporan lokasi baru.

Mengembalikan nilai

Jika metode ini berhasil, metode ini mengembalikan S_OK. Jika tidak, kode kesalahan HRESULT akan dikembalikan.

Keterangan

ILocationReport adalah antarmuka dasar dari jenis laporan lokasi tertentu. Antarmuka aktual yang diterima pemanggil untuk pLocationReport akan cocok dengan jenis yang ditentukan oleh reportType.

Jika aplikasi memanggil OnLocationChanged sebagai akibat dari penggunaan lokasi pertamanya, panggilan dapat menyebabkan pemberitahuan muncul di taskbar, dan menyebabkan peristiwa Aktivitas Lokasi masuk Pemantau Peristiwa.

Catatan Aplikasi tidak menerima peristiwa perubahan lokasi yang diharapkan dari OnLocationChanged jika kedua kondisi berikut ini benar. Pertama, aplikasi berjalan sebagai layanan, dalam konteks akun pengguna LOCALSERVICE, SYSTEM, atau NETWORKSERVICE. Kedua, lokasi mengubah hasil peristiwa dari mengubah lokasi default, baik secara manual saat pengguna memilih Lokasi Default di Panel Kontrol, atau secara terprogram saat aplikasi memanggil IDefaultLocation::SetReport.
 

Contoh

Contoh implementasi OnLocationChanged berikut menangani peristiwa perubahan lokasi untuk laporan lintang/bujur. Implementasi ini mencetak informasi berikut tentang peristiwa perubahan lokasi lintang/bujur: tanda waktu, ID sensor, garis lintang, bujur, radius kesalahan, ketinggian, dan kesalahan ketinggian.

// 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;
}

Persyaratan

Persyaratan Nilai
Klien minimum yang didukung Windows 7 [hanya aplikasi desktop],Windows 7
Server minimum yang didukung Tidak ada yang didukung
Target Platform Windows
Header locationapi.h
DLL LocationAPI.dll