-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoCoordinateWatcher.cpp
66 lines (58 loc) · 1.71 KB
/
GeoCoordinateWatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "GeoCoordinateWatcher.h"
IID REPORT_TYPES[] = { IID_ILatLongReport };
GeoCoordinateWatcher* GeoCoordinateWatcher::_instance;
GeoCoordinateWatcher::GeoCoordinateWatcher()
{
_instance = this;
hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);;
if (SUCCEEDED(hr))
{
hr = spLocation.CoCreateInstance(CLSID_Location);
}
}
GeoCoordinateWatcher::~GeoCoordinateWatcher()
{
if (SUCCEEDED(hr))
{
for (auto index : REPORT_TYPES)
{
spLocation->UnregisterForReport(index);
}
}
if (nullptr != pLocationEvents)
{
pLocationEvents->Release();
pLocationEvents = nullptr;
}
}
GeoCoordinateWatcher* GeoCoordinateWatcher::GetInstance()
{
if (_instance == nullptr)
_instance = new GeoCoordinateWatcher();
return _instance;
}
void GeoCoordinateWatcher::onPositionChange(bool isHighAccuracy, std::function<void(GeoCoordinate)> func)
{
this->callback = std::move(func);
if (SUCCEEDED(hr))
{
hr = CComObject<CLocationEvents>::CreateInstance(&pLocationEvents);
if (nullptr != pLocationEvents)
{
pLocationEvents->AddRef();
}
}
if (SUCCEEDED(hr))
{
hr = spLocation->RequestPermissions(nullptr, REPORT_TYPES, ARRAYSIZE(REPORT_TYPES), FALSE);
if (FAILED(hr))
{
wprintf(L"Warning: Unable to request permissions.\n");
}
for (auto index : REPORT_TYPES)
{
spLocation->SetDesiredAccuracy(IID_ILatLongReport, isHighAccuracy ? LOCATION_DESIRED_ACCURACY_HIGH : LOCATION_DESIRED_ACCURACY_DEFAULT);
hr = spLocation->RegisterForReport(pLocationEvents, index, 0);
}
}
}