【问题标题】:Is it possible to change DesiredAccuracy & ReportInterval of Geolocator in PositionChanged Event handler?是否可以在 PositionChanged 事件处理程序中更改 Geolocator 的 DesiredAccuracy 和 ReportInterval?
【发布时间】:2012-12-05 10:21:48
【问题描述】:

我想改变PositionChanged Event Handler中的DesiredAccuracy和ReportInterval,以便动态改变不同位置的位置更新频率。

我做了这样的事情,

void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    geolocator.StatusChanged -= geolocator_StatusChanged;
    geolocator.PositionChanged -= geolocator_PositionChanged;
    geolocator.DesiredAccuracy = PositionAccuracy.High;
    geolocator.ReportInterval = 5 * 1000;
    geolocator.StatusChanged += geolocator_StatusChanged;
    geolocator.PositionChanged += geolocator_PositionChanged;
}

但问题是我得到了

$exception {System.Exception: 操作中止(来自 HRESULT 的异常:0x80004004 (E_ABORT))

Windows.Devices.Geolocation.Geolocator.put_DesiredAccuracy(PositionAccuracy 值)

我不理解这个异常,因为它没有说明原因。

如何实现这一点(动态更改准确性和报告间隔)?

谢谢。

【问题讨论】:

    标签: windows silverlight gps windows-phone windows-phone-8


    【解决方案1】:

    根据this Microsoft article,您的异常表明您已从手机设置中禁用定位服务:

    catch (Exception ex)
    {
        if ((uint)ex.HResult == 0x80004004)
        {
            // the application does not have the right capability or the location master switch is off
            StatusTextBlock.Text = "location  is disabled in phone settings.";
        }
        //else
        {
            // something else happened acquring the location
        }
    }
    

    【讨论】:

      【解决方案2】:

      最好在更改这些属性之前使用 GeoCoordinateWatcher 并调用 Stop()/Start()。有 a few advantages 使用 GeoLocator 而不是 GeoCoordinateWatcher,但对于大多数应用程序来说并不重要。由于 WP8 仍完全支持 GeoCoordinateWatcher,如果可行,您可能更容易切换到它。

      【讨论】:

        【解决方案3】:

        正如您在代码中所做的那样,需要删除 StatusChangedPositionChanged 的所有处理程序才能修改 ReportInterval 等。否则将引发此异常 (0x80004004)。

        就我而言,删除所有处理程序不是一个选项,因为我使用的是Geolocatorin the my WP8 app to keep my app alive in background。删除最后一个处理程序也会暂停我的应用程序,因为从 WP 的角度来看,没有理由让应用程序在后台保持活动状态。

        我发现可以通过创建一个临时的Geolocator 来解决这个问题:

        // Wire up a temporary Geolocator to prevent the app from closing
        var tempGeolocator = new Geolocator
        {
            MovementThreshold = 1,
            ReportInterval = 1
        };
        TypedEventHandler<Geolocator, PositionChangedEventArgs> dummyHandler = (sender, positionChangesEventArgs2) => { };
        tempGeolocator.PositionChanged += dummyHandler;
        
        Geolocator.PositionChanged -= OnGeolocatorOnPositionChanged;
        Geolocator.ReportInterval = reportInterval;
        Geolocator.PositionChanged += OnGeolocatorOnPositionChanged;
        
        tempGeolocator.PositionChanged -= dummyHandler;
        

        这样应用程序就不会被 WP 杀死。请记住,重新绑定 PositionChanged 也会导致立即回调。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多