次の方法で共有


ILocationEvents::OnLocationChanged メソッド (locationapi.h)

[Win32 Location API は、[要件] セクションで指定されたオペレーティング システムで使用できます。 今後のバージョンでは変更されるか、利用できなくなる場合もあります。 代わりに、 Windows.Devices.Geolocation API を使用します。 ]

新しい場所レポートが使用可能な場合に呼び出されます。

構文

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

パラメーター

[in] reportType

pLocationReport に含まれるレポートの種類のインターフェイス ID を含む REFIID

[in] pLocationReport

新しい場所レポートを含む ILocationReport インスタンスへのポインター。

戻り値

このメソッドは、成功すると S_OK を返します。 そうでない場合は、HRESULT エラー コードを返します。

注釈

ILocationReport は、特定の場所レポートの種類の基本インターフェイスです。 呼び出し元が pLocationReport に対して受け取る実際のインターフェイスは、 reportType で指定された型と一致します。

場所の最初の使用の結果としてアプリケーションが OnLocationChanged を呼び出した場合、呼び出しによってタスク バーに通知が表示され、Location Activity イベントがイベント ビューアーに記録される可能性があります。

メモ 次の条件が両方とも当てはまる場合、アプリケーションは OnLocationChanged から予期される場所変更イベントを受け取りません。 まず、アプリケーションは、LOCALSERVICE、SYSTEM、または NETWORKSERVICE ユーザー アカウントのコンテキストでサービスとして実行されます。 次に、場所の変更イベントは、ユーザーが コントロール パネル の既定の場所を選択したときに手動で、またはアプリケーションが IDefaultLocation::SetReport を呼び出すときにプログラムによって既定の場所を変更した結果です。
 

OnLocationChanged の次の実装例は、緯度/経度レポートの場所変更イベントを処理します。 この実装では、緯度/経度の場所変更イベントに関する次の情報を出力します。タイムスタンプ、センサー ID、緯度、経度、誤差半径、高度、高度エラー。

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

要件

要件
サポートされている最小のクライアント Windows 7 [デスクトップ アプリのみ],Windows 7
サポートされている最小のサーバー サポートなし
対象プラットフォーム Windows
ヘッダー locationapi.h
[DLL] LocationAPI.dll