我如何使用通用Windows应用程序(UWP)在地图中跟踪用户?

时间:2016-09-07 08:18:43

标签: uwp uwp-maps

MainPage.xaml中

    public double lat;
    public double lon;
    public int Accurancy;
    // Proides access to location data
    private Geolocator _geolocator = null;


    private void UpdateLocationData(Geoposition position)
    {
        if (position == null)
        {
            lat = 0;
            lon = 0;
            Accurancy = 0;
        }
        else
        {
            lat = position.Coordinate.Point.Position.Latitude;
            lon = position.Coordinate.Point.Position.Longitude;
            Accurancy = Convert.ToInt32(position.Coordinate.Accuracy);
            AddLocation(lat, lon);
        }
    }

    private void AddLocation(double s,double s2)
    {
        BasicGeoposition location = new BasicGeoposition();
        location.Latitude = s;
        location.Longitude = s2;
        MapIcon mapIcon;
        mapIcon = new MapIcon();
        if (mapIcon != null)
        {
            MapControl1.MapElements.Remove(mapIcon);
        }
        mapIcon.Location = new Geopoint(location);
        mapIcon.Title = "Azar";
        MapControl1.MapElements.Add(mapIcon);
        MapControl1.Center = new Geopoint(location);
    }

    private async void TrackLocationButton_Click(object sender, RoutedEventArgs e)
    {
        // Request permission to access location
        var accessStatus = await Geolocator.RequestAccessAsync();

        switch (accessStatus)
        {
            case GeolocationAccessStatus.Allowed;
            _geolocator = new Geolocator { ReportInterval = 2000 };
            _geolocator.PositionChanged += OnPositionChanged;
            _geolocator.StatusChanged += OnStatusChanged;            
        }
    }

    private async void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            switch (e.Status)
            {
                case PositionStatus.Ready:
                      // message
                    break;

                case PositionStatus.Initializing:
                   // message
                    break;

                case PositionStatus.NoData:
                   // message
                    break;

                case PositionStatus.Disabled:
                // message
                    break;

                case PositionStatus.NotInitialized:
                  // message
                    break;

                case PositionStatus.NotAvailable:
                    // message
                    break;

                default:
                    // message
                    break;
            }
        });
    }


    private async void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            UpdateLocationData(e.Position);
        });

    }

Mainpage.cs

{{1}}

我尝试跟踪用户并在地图控件中固定起点和终点的路线,然后我想用户在路线上行驶,如果用户更改路径,我会收到警报吗?那怎么办呢?

1 个答案:

答案 0 :(得分:0)

地图控件当前没有用于显示用户位置或跟踪它们的当前用户位置或API的概念。调用应用程序通过RouteView对象或直接添加折线来添加路由。