导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

Geolocator.GetGeopositionAsync() | getGeopositionAsync() Method

启动检索设备当前位置的异步操作。

语法


geolocator.getGeopositionAsync().done( /* 你的成功和错误处理程序 */ );

参数

此 方法 没有参数。

返回值

类型: IAsyncOperation<Geoposition>

提供用于启动位置数据的异步请求和处理其完成操作的方法。

备注

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

当在联接的备用时,Geolocator 对象始终可以实例化,但 Geolocator 对象将不会找到任何传感器来聚合并调用 GetGeopositionAsync 在 7 秒后将超时,将不会调用 PositionChanged 事件侦听器,并且 StatusChanged 事件侦听器将以 NoData 状态一次调用。

示例

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

命名空间

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

元数据

Windows.winmd

Capabilities

ID_CAP_LOCATION [Windows Phone]

另请参见

Geoposition
Geocoordinate
Geolocator
GetGeopositionAsync(TimeSpan, TimeSpan)

 

 

Microsoft 正在进行一项网上调查,以了解您对 MSDN 网站的意见。 如果您选择参加,我们将会在您离开 MSDN 网站时向您显示该网上调查。

是否要参加?
显示:
© 2015 Microsoft