Skip to main content
Geolocator.GetGeopositionAsync() | getGeopositionAsync() method

Applies to Windows and Windows Phone

Starts an asynchronous operation to retrieve the current location of the device.

Syntax


geolocator.getGeopositionAsync().done( /* Your success and error handlers */ );

public IAsyncOperation<Geoposition> GetGeopositionAsync()

Public Function GetGeopositionAsync() As IAsyncOperation(Of Geoposition)

public:
IAsyncOperation<Geoposition>^ GetGeopositionAsync()

Parameters

This method has no parameters.

Return value

Type: IAsyncOperation<Geoposition>

Provides methods for starting the asynchronous request for location data and handling its completion.

Remarks

  • Applies to Windows

The first use of the Geolocator object to call GetGeopositionAsync must be made on the UI thread so that the consent prompt can be shown to the user. For more information, see Guidelines for devices that access personal data.

  • Applies to Windows

When in Connected Standby, Geolocator objects can always be instantiated but the Geolocator object will not find any sensors to aggregate and calls to GetGeopositionAsync will time out after 7 seconds, PositionChanged event listeners will never be called, and StatusChanged event listeners will be called once with the NoData status.

Examples

This example shows how to call GetGeopositionAsync.


    var loc = null;

    function getloc() {
        if (loc == null) {
            loc = new Windows.Devices.Geolocation.Geolocator();
        }
        if (loc != null) {
            loc.getGeopositionAsync().then(getPositionHandler, errorHandler);
        }
    }

    function getPositionHandler(pos) {
        document.getElementById('latitude').innerHTML = pos.coordinate.latitude;
        document.getElementById('longitude').innerHTML = pos.coordinate.longitude;
        document.getElementById('accuracy').innerHTML = pos.coordinate.accuracy;
        document.getElementById('geolocatorStatus').innerHTML =
                getStatusString(loc.locationStatus);
    }

    function errorHandler(e) {
        document.getElementById('errormsg').innerHTML = e.message;
        // Display an appropriate error message based on the location status.
        document.getElementById('geolocatorStatus').innerHTML =
            getStatusString(loc.locationStatus);
    }

    function getStatusString(locStatus) {
        switch (locStatus) {
            case Windows.Devices.Geolocation.PositionStatus.ready:
                // Location data is available
                return "Location is available.";
                break;
            case Windows.Devices.Geolocation.PositionStatus.initializing:
                // This status indicates that a location device is still initializing
                return "Location devices are still initializing.";
                break;
            case Windows.Devices.Geolocation.PositionStatus.noData:
                // No location data is currently available 
                return "Data from location services is currently unavailable.";
                break;
            case Windows.Devices.Geolocation.PositionStatus.disabled:
                // The app doesn't have permission to access location,
                // either because location has been turned off.
                return "Your location is currently turned off. " +
                    "Change your settings through the Settings charm " +
                    " to turn it back on.";
                break;
            case Windows.Devices.Geolocation.PositionStatus.notInitialized:
                // This status indicates that the app has not yet requested
                // location data by calling GetGeolocationAsync() or 
                // registering an event handler for the positionChanged event. 
                return "Location status is not initialized because " +
                    "the app has not requested location data.";
                break;
            case Windows.Devices.Geolocation.PositionStatus.notAvailable:
                // Location is not available on this version of Windows
                return "You do not have the required location services " +
                    "present on your system.";
                break;
            default:
                break;
        }
    }

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core;
using Windows.Devices.Geolocation;
using System.Threading;
using System.Threading.Tasks;

namespace GeolocationApp
{
    public sealed partial class MainPage : Page
    {
        private Geolocator geolocator = null;

        public MainPage()
        {
            this.InitializeComponent();
            geolocator = new Geolocator();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            btnGetLocation.IsEnabled = true;
        }


        async private void btnGetLocation_Click(object sender, RoutedEventArgs e)
        {
            btnGetLocation.IsEnabled = false;

            try
            {
                // Carry out the operation
                Geoposition pos = await geolocator.GetGeopositionAsync();

                tbLatitude.Text = pos.Coordinate.Latitude.ToString();
                tbLongitude.Text = pos.Coordinate.Longitude.ToString();
                tbAccuracy.Text = pos.Coordinate.Accuracy.ToString();
                tbStatus.Text = GetStatusString(geolocator.LocationStatus);
            }
            catch (System.UnauthorizedAccessException)
            {
                tbLatitude.Text = "No data";
                tbLongitude.Text = "No data";
                tbAccuracy.Text = "No data";
                tbStatus.Text = "No data";
            }
            catch (TaskCanceledException)
            {
                tbLatitude.Text = "Cancelled";
                tbLongitude.Text = "Cancelled";
                tbAccuracy.Text = "Cancelled";
                tbStatus.Text = "Cancelled";
            }

            btnGetLocation.IsEnabled = true;
        }


         private string GetStatusString(PositionStatus status)
        {
                var strStatus = "";

                switch (status)
                {
                    case PositionStatus.Ready:
                        strStatus = "Location is available.";
                        break;

                    case PositionStatus.Initializing:
                        strStatus = "Geolocation service is initializing.";
                        break;

                    case PositionStatus.NoData:
                        strStatus = "Location service data is not available.";
                        break;

                    case PositionStatus.Disabled:
                        strStatus = "Location services are disabled. Use the " +
                                    "Settings charm to enable them.";
                        break;

                    case PositionStatus.NotInitialized:
                        strStatus = "Location status is not initialized because " +
                                    "the app has not yet requested location data.";
                        break;

                    case PositionStatus.NotAvailable:
                        strStatus = "Location services are not supported on your system.";
                        break;

                    default:
                        strStatus = "Unknown PositionStatus value.";
                        break;
                }

                return (strStatus);

        }

    }
}

Requirements

Minimum supported client

Windows 8

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8

Namespace

Windows.Devices.Geolocation
Windows::Devices::Geolocation [C++]

Metadata

Windows.winmd

Capabilities

ID_CAP_LOCATION [Windows Phone]

See also

Geoposition
Geocoordinate
Geolocator
GetGeopositionAsync(TimeSpan, TimeSpan)