此页面有用吗?
您对此内容的反馈非常重要。 请告诉我们您的想法。
更多反馈?
1500 个剩余字符
导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

Geolocator Class

提供对当前地理位置的访问。

语法


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

特性

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

成员

Geolocator类 具有这些类型的成员:

构造函数

Geolocator类 具有这些构造函数。

构造函数说明
Geolocator Initializes a new Geolocator object.

 

事件

Geolocator类 具有这些事件。

事件说明
PositionChanged Raised when the location is updated.
StatusChanged Raised when the ability of the Geolocator to provide updated location changes.

 

方法

The Geolocator 类 具有这些方法。 对于 C#、Visual Basic 和 C++,它还继承方法于Object 类.

方法说明
GetGeopositionAsync() Starts an asynchronous operation to retrieve the current location of the device.
GetGeopositionAsync(TimeSpan, TimeSpan) Starts an asynchronous operation to retrieve the current location of the device.

 

属性

Geolocator类 具有这些属性。

属性访问类型说明

DesiredAccuracy

读/写The accuracy level at which the Geolocator provides location updates.

DesiredAccuracyInMeters

读/写Gets or sets the desired accuracy in meters for data returned from the location service.

LocationStatus

只读The status that indicates the ability of the Geolocator to provide location updates.

MovementThreshold

读/写Gets 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

读/写The 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.

 

备注

必须在 UI 线程上首次使用 Geolocator 对象调用 GetGeopositionAsync 或订阅 PositionChanged 事件,以便同意提示可以显示给用户。有关更多信息,请参见访问私有数据的设备的准则

示例

此示例演示如何调用 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;
        }
    }


要求

最低支持客户端

Windows 8 [仅 Windows 应用商店应用程序]

最低支持服务器

不支持“无” [仅 Windows 应用商店应用程序]

最低支持电话

Windows Phone 8

命名空间

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

元数据

Windows.winmd

Capabilities

location
ID_CAP_LOCATION [Windows Phone]

 

 

显示:
© 2015 Microsoft