Geolocator class

0 out of 6 rated this helpful - Rate this topic

Provides access to the current geographic location.

Syntax


var geolocator = new Windows.Devices.Geolocation.Geolocator();

Attributes

ActivatableAttribute(NTDDI_WIN8)
MarshalingBehaviorAttribute(Agile)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)

Members

The Geolocator class has these types of members:

Constructors

The Geolocator class has these constructors.

ConstructorDescription
Geolocator Initializes a new Geolocator object.

 

Events

The Geolocator class has these events.

EventDescription
PositionChanged Raised when the location is updated.
StatusChanged Raised when the ability of the Geolocator to provide updated location changes.

 

Methods

The Geolocator class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
GetGeopositionAsync() Starts an asynchronous operation to retrieve the location of the user's computer.
GetGeopositionAsync(TimeSpan, TimeSpan) Starts an asynchronous operation to retrieve the current location.

 

Properties

The Geolocator class has these properties.

PropertyAccess typeDescription

DesiredAccuracy

Read/writeThe accuracy level at which the Geolocator provides location updates.

DesiredAccuracyInMeters

Read/writeWindows Phone only. Gets or sets the desired accuracy in meters for data returned from the location service.

LocationStatus

Read-onlyThe status that indicates the ability of the Geolocator to provide location updates.

MovementThreshold

Read/writeGets and sets the distance of movement, in meters, relative to the coordinate from the last PositionChanged event, that is required for the Geolocator to raise a PositionChanged event.

ReportInterval

Read/writeThe requested minimum time interval between location updates, in milliseconds. If your application requires updates infrequently, set this value so that the location provider can conserve power by calculating location only when needed.

 

Remarks

The first use of the Geolocator object to call GetGeopositionAsync or subscribe to PositionChanged events 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.

Examples

This example shows how to call GetGeopositionAsync.



<!DOCTYPE html>
<html>
<head>
<title>Windows Geolocation Example</title>
<link rel="stylesheet" href="/winjs/css/ui-dark.css" />
<script type = "text/javascript" >

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;
    }
}
</script>
</head>
<body>
<p>
Click "Get Location" to get geolocation data.<br />
<input type="button" onclick="getloc()" value="Get Location" /><br />

Latitude: <span id="latitude"></span><br />
Longitude: <span id="longitude"></span><br />
Accuracy (in meters): <span id="accuracy"></span><br /><br />
Location Status:<span id="geolocatorStatus"></span><br />
Error Message: <span id="errormsg"></span><br />
</p>
</body>
</html>

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8

Namespace

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

Metadata

Windows.winmd

Capabilities

location
ID_CAP_LOCATION [Windows Phone]

 

 

Build date: 2/25/2013

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.