【问题标题】:Geolocator PositionChanged event not firing for getting current locationGeolocator PositionChanged 事件未触发以获取当前位置
【发布时间】:2018-07-17 10:03:38
【问题描述】:

我正在尝试获取 WinRT 应用程序的当前位置。 但是 PositionChanged 事件永远不会被触发。 非常感谢任何帮助!

在 Package.appxmanifest 中,我启用了位置。 我的代码如下:-

    Geolocator geolocator = new Geolocator { ReportInterval = 2000 };
    geolocator.DesiredAccuracyInMeters = 100;  // specify your range
    geolocator.PositionChanged += OnPositionChanged;

private async void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
        {
            if (this.previousLocationState == LocationStates.PopupEmpty && (viewModel.Coordinate == null || viewModel.Coordinate.Latitude != prevGpsCoordinate.Latitude || viewModel.Coordinate.Longitude != prevGpsCoordinate.Longitude))
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    this.viewModel.Coordinate = new GpsCoordinate { Latitude = e.Position.Coordinate.Point.Position.Latitude, Longitude = e.Position.Coordinate.Point.Position.Longitude };
                    Field.GpsCoordinate gpsCoord = new Field.GpsCoordinate { Latitude = viewModel.Coordinate.Latitude, Longitude = viewModel.Coordinate.Longitude };
                    CreateMapPin(gpsCoord);
                    prevGpsCoordinate = this.viewModel.Coordinate;
                });
            }
        }

【问题讨论】:

  • 你事先调用过静态方法RequestAccessAsync吗?
  • 嗨@SteveJ Geolocator 类中没有名为 RequestAccessAsync 的方法
  • 它是静态的。您不会在实例上调用它。
  • 没有这样的静态方法可用...只有 Equals 和 ReferenceEquals 静态方法可用。

标签: c# location winrt-async


【解决方案1】:

我认为你需要这样做:

await Geolocator.RequestAccessAsync();
Geolocator geolocator = new Geolocator { ReportInterval = 2000 };
geolocator.DesiredAccuracyInMeters = 100;  // specify your range
geolocator.PositionChanged += OnPositionChanged;
await geolocator.GetGeopositionAsync();

将此作为答案,因为我无法在评论中发布代码。

【讨论】:

  • 我使用的是 Windows 8.1,并且我已将位置隐私设置设为开启。 var geoposition = await geolocator.GetGeopositionAsync();这会检查我的位置是否已启用。但是出现“您的应用无权访问位置数据的权限。请确保您已在应用程序清单中定义 ID_CAP_LOCATION 并且在您的手机上,您已通过检查设置 > 位置打开位置。”
  • 您能否将启用位置功能的清单部分添加到问题中?
【解决方案2】:

我通过将此代码放入 Package.appxmanifest 中使其工作

<DeviceCapability Name="location" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多