Windows 8 WinRT GetGeopositionAsync works in JavaScript not in C++?

Windows 8 WinRT GetGeopositionAsync works in JavaScript not in C++?,windows-8,windows-runtime,microsoft-metro,Windows 8,Windows Runtime,Microsoft Metro,Just curious if this is a bug or if I'm doing it wrong. I'm trying to use the Geolocator from WinRT. I have a test Metro app in Javascript and everything works with this code: var locator = Windows.Devices.Geolocation.Geolocator(); var promise = locator.getGeopositionAsync().then( function (pos) { Loc.innerText = "Lat: " + pos.coordinate.latitude + ", Lng: " + pos.coordinate.longitude; }); I'm trying to do the same thing in C++ app with this code but it doesn't ever enter my

Just curious if this is a bug or if I'm doing it wrong. I'm trying to use the Geolocator from WinRT. I have a test Metro app in Javascript and everything works with this code:

var locator = Windows.Devices.Geolocation.Geolocator();
var promise = locator.getGeopositionAsync().then(
    function (pos) {
        Loc.innerText = "Lat: " +  pos.coordinate.latitude + ", Lng: " + pos.coordinate.longitude;
    });

I'm trying to do the same thing in C++ app with this code but it doesn't ever enter my lambda:

auto locator = ref new Geolocator();
auto operation = locator->GetGeopositionAsync();
operation->Completed =  ref new AsyncOperationCompletedHandler< Geoposition^ >(
    [=](IAsyncOperation<Geoposition^>^ operation)
    {
        auto result = operation->GetResults(); 
        std::wstringstream ss;
        ss << L"Lat: " << result->Coordinate->Latitude << L", Lng: " << result->Coordinate->Longitude;
        this->Loc->Text = ref new String( ss.str().c_str() );
    });

I've turned on the Location capability in both apps. Any suggestions?


#1

You could give it a try using the PPL. In my experience that's much nicer to develop with in C++ than using these handlers.

#include <ppl>

Concurrency::task<Geoposition^> getPositionTask(locator->GetGeopositionAsync());
getPositionTask.then([=](IAsyncOperation<Geoposition^>^ operation) {
...

Of course, if you're developing a component, you can't return Concurrency::task objects from your methods, but as long as you're within your code, they are probably preferable.


#2

You could give it a try using the PPL. In my experience that's much nicer to develop with in C++ than using these handlers.

#include <ppl>

Concurrency::task<Geoposition^> getPositionTask(locator->GetGeopositionAsync());
getPositionTask.then([=](IAsyncOperation<Geoposition^>^ operation) {
...

Of course, if you're developing a component, you can't return Concurrency::task objects from your methods, but as long as you're within your code, they are probably preferable.


#3

Curious, does your operation object contain an available "Start" method (show via intellisense)?

#4

Have you declared location capability for the C++ app?

#5

@iaimtomisvehave Yes: "I've turned on the Location capability in both apps."

#6

@Bob Delavan Yes: It has a Start() method, but I'm not calling Start(). The example that I was using when I wrote the C++ version didn't call Start either: msdn.microsoft.com/en-us/magazine/hh781020.aspx

#7

If that's the case, I was wondering if calling the start as the last line of the code you provided would cause it to work. The current developer preview documentation calls it out as starting the request for the location. msdn.microsoft.com/en-us/library/windows/apps/…

#8

Curious, does your operation object contain an available "Start" method (show via intellisense)?

#9

Have you declared location capability for the C++ app?

#10

@iaimtomisvehave Yes: "I've turned on the Location capability in both apps."

#11

@Bob Delavan Yes: It has a Start() method, but I'm not calling Start(). The example that I was using when I wrote the C++ version didn't call Start either: msdn.microsoft.com/en-us/magazine/hh781020.aspx

#12

If that's the case, I was wondering if calling the start as the last line of the code you provided would cause it to work. The current developer preview documentation calls it out as starting the request for the location. msdn.microsoft.com/en-us/library/windows/apps/…