Geolocator PositionChanged事件未触发以获取当前位置

时间:2018-07-17 10:03:38

标签: c# location winrt-async

我正在尝试获取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;
                });
            }
        }

2 个答案:

答案 0 :(得分:0)

我认为您需要这样做:

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

将其作为答案,因为我无法在注释中发布代码。

答案 1 :(得分:0)

通过将这段代码放在Package.appxmanifest中使它起作用

       IStudentRepository _repository=new StudentRepository();
    public HomeController(StudentRepository _repository)
    {
        this._repository = _repository;
    }
    public JsonResult Index()
    {
       var m= _repository.getAll();
       return Json(m,JsonRequestBehavior.AllowGet);
    }