Game Development Reference
In-Depth Information
Polling
Polling for the geolocation is a bit different from the sensor system, which just
provides the latest value. If you want to retrieve the location from the GPS, you need
to initiate a request, which the GPS will try to fulfill as soon as possible. This means
that you need to use an asynchronous method to access this data, using the Task
APIs in WinRT.
To do this, we will create a Geolocator as was done earlier; however, when you
want to retrieve a location value you need to create a new task<Geoposition^> ,
which handles calling the geolocator->GetGeopositionAsync() method. This
will immediately return; however, you won't get any data out of it until the operation
completes. You have two options here. You can use the blocking task::wait()
call to wait for it to complete (not recommended, as it may cause performance hic-
cups), and then retrieve the Geoposition using the task::get() method. Altern-
atively, you can use the task::then() method to define some code that will run
after the Geoposition is acquired, in which you can update your game using the
location provided, as shown:
task<Geoposition^>
task(geolocation->GetGeopositionAsync());
task.then([this](task<Geoposition^> getPosTask)
{
Geoposition^ pos = getPosTask.get();
// Do what you need to do with the position
};
As shown in the given code, you can create the task, pass in the GetGeoposi-
tionAsync() method, and call the task::then() method, which indicates what
to do once the task returns. We can pass in a method to it; however, in this case I've
used a lambda to inline the code.
Search WWH ::




Custom Search