none
Enable location & return to app

    Question

  • Hi,

    My app need to access location service, for that I am asking user to whether to enable location, if user says yes, then I am opening location settings. Upto this is working. But I suppose to handle when user coming from location page to my application page. 

    Here is my code

    private async Task<Geoposition> getCurrentLocation()
            {
                Geoposition position = null;
                Geolocator locator = new Geolocator() { DesiredAccuracyInMeters = 10 };
                var flag = true;
                try
                {
                    position = await locator.GetGeopositionAsync(TimeSpan.FromHours(1), TimeSpan.FromSeconds(30));
                    flag = false;
                }
                catch (Exception uae)
                {
                    
                }
                if (flag)
                {
                    await ShowLocationPage();
                }
            }

    getCurrentLocation method called when page is loaded. If it didnt get user location then ShowLocationPage method get called.

    private static async Task ShowLocationPage() { ContentDialog cd = new ContentDialog(){ Content = "Application want to access your location. Would you like to turn on Location Services?", PrimaryButtonText = "Yes", SecondaryButtonText = "No" }; var result = await cd.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                  var x = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
                }

    }

    Now how I suppose to handle when user return from location page to my page.



    • Edited by Anieruddha Wednesday, August 13, 2014 12:44 PM
    Wednesday, August 13, 2014 12:31 PM

Answers

  • I suppose you want to get the location when the user back to your app.

    I think you can overried OnNavigatedTo in main page and call GetGeopositionAsync again. if it still throws expection, you can stop doing this.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 13, 2014 5:09 PM
    Moderator
  • OnNavigatedTo is called each time you navigate to the page even if you return to it, Alan's solution will work just fine for you.

    You'll have a code like this :

    private bool NavLocationSettings=false;
    
    protected override OnNavigatedTo(... ... ...)
    {
        //NavLocationSettings will only be true if you return from the LocationSettings page
        if(NavLocationSettings)
        {
            NavLocationSettings= false;
            ... // getCurrentLocation
        }
    }
    
    // set the NavLocationSettings to true
    private static async Task ShowLocationPage()
    {
        ...
        if(result == ConetntDialogResult.Primary)
        {
            NavLocationSettings = true;
            var x = await ...
        }
    }

    I hope it helps,

    Yassine

    Sunday, August 17, 2014 4:47 PM

All replies

  • I suppose you want to get the location when the user back to your app.

    I think you can overried OnNavigatedTo in main page and call GetGeopositionAsync again. if it still throws expection, you can stop doing this.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 13, 2014 5:09 PM
    Moderator
  • I suppose you want to get the location when the user back to your app.

    I think you can overried OnNavigatedTo in main page and call GetGeopositionAsync again. if it still throws expection, you can stop doing this.




    OnNavigatedTo get called when I start application, but not when I return from location page to application.
    Sunday, August 17, 2014 11:54 AM
  • OnNavigatedTo is called each time you navigate to the page even if you return to it, Alan's solution will work just fine for you.

    You'll have a code like this :

    private bool NavLocationSettings=false;
    
    protected override OnNavigatedTo(... ... ...)
    {
        //NavLocationSettings will only be true if you return from the LocationSettings page
        if(NavLocationSettings)
        {
            NavLocationSettings= false;
            ... // getCurrentLocation
        }
    }
    
    // set the NavLocationSettings to true
    private static async Task ShowLocationPage()
    {
        ...
        if(result == ConetntDialogResult.Primary)
        {
            NavLocationSettings = true;
            var x = await ...
        }
    }

    I hope it helps,

    Yassine

    Sunday, August 17, 2014 4:47 PM