locked
Finding the current location for Windows 8 App

    Question

  • I need to find the current location on windows 8 App but its always results in error.

    Here is the code i am using :

    try {           
        var loc = Windows.Devices.Geolocation.Geolocator();
        loc.getGeopositionAsync().then(getPositionHandler, errorHandler);
        } catch (e) {
        // Catch Errors
    }
    

    This always results in errorHandler with the error "cancelled".

    Also i am trying this on a laptop. Any idea why this is not working ?

    Thanks.

    Friday, October 11, 2013 11:10 AM

Answers

All replies

  • did you changed the manifest and add the capability? can you send a repro project that we can try out?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, October 11, 2013 12:00 PM
  • This happens because the loc variable goes out of scope before the async operation is completed, resulting in a cancelation. This behavior is particular to the geolocator, and is something I've confirmed with the engineering team. I wrote about this on my blog as well: http://kraigbrockschmidt.com/blog/?p=1120.

    Put the var loc = line outside the function so it stays in scope, and it should work for you.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview


    • Marked as answer by rkagrawal Tuesday, October 15, 2013 7:00 AM
    Friday, October 11, 2013 3:40 PM
  • Thanks Kraig.
    Tuesday, October 15, 2013 6:59 AM