主页.xaml
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 0 0 0">
<Button x:Name="TrackLocationButton" Margin="120 20 0 0" Content="track location" Click="TrackLocationButton_Click"/>
</StackPanel>
<Maps:MapControl x:Name="MapControl1" HorizontalAlignment="Left" Height="500" Margin="30 100 0 0" Width="300" VerticalAlignment="Top"/>
主页.cs
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);
});
}
我尝试跟踪用户并在地图控件中固定起点和目的地点的路线,然后我想在用户更改路径时让用户旅行路线,我得到警报?那我该怎么做呢?