Windows Phone 使图钉图标在地图上的开始和结束位置之间移动
Windows Phone make pushpin icon move on map between start and end location
我正在创建一个应用程序,当用户在他的路径上行驶时,我试图在其中移动自行车图标。我通过订阅 Geolocator class 的位置更改事件来获取用户的新位置。我每次都将图钉设置到这个新位置。但问题是图钉不会像动画那样在该路径上移动,而是突然移动到那个新位置。有什么方法可以使图钉动画以使用起点和终点沿着路径移动。这还应该包括像打开弯曲路径这样的场景。
当前代码:
XAML:
<maps:Map x:Name="myMap" ZoomLevel="16">
<toolkit:MapExtensions.Children>
<toolkit:UserLocationMarker x:Name="UserLocationMarker" />
<!--<toolkit:Pushpin x:Name="MyPushpin" Content="My Position"></toolkit:Pushpin>-->
</toolkit:MapExtensions.Children>
</maps:Map>
C#:
Geolocator geolocator = new Geolocator();
this.geolocator.MovementThreshold = 0;
RouteQuery MyQuery = null;
GeocodeQuery Mygeocodequery = null;
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
geolocator.PositionChanged += geolocator_PositionChanged;
Geoposition geoposition = await geolocator.GetGeopositionAsync();
myMap.Center = geoposition.Coordinate.ToGeoCoordinate();
}
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
UserLocationMarker marker = (UserLocationMarker)this.FindName("UserLocationMarker");
marker.GeoCoordinate = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
});
}
private void MovePinOnPath(bool isGeodesic)
{
ClearMap();
Pushpin marker = (Pushpin)this.FindName("MyPushpin");
marker.GeoCoordinate = path[0];
currentAnimation = new Animations.PathAnimation(path, (coord, pathIdx, frameIdx) =>
{
marker.GeoCoordinate = coord;
}, isGeodesic, 10000);
currentAnimation.Play();
}
private void ClearMap()
{
if (currentAnimation != null)
{
currentAnimation.Stop();
currentAnimation = null;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MovePinOnPath(true);
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrWhiteSpace(txtSearchPlace.Text))
{
Mygeocodequery = new GeocodeQuery();
Mygeocodequery.SearchTerm = txtSearchPlace.Text;
Mygeocodequery.GeoCoordinate = new GeoCoordinate(myMap.Center.Latitude, myMap.Center.Longitude);
Mygeocodequery.QueryCompleted += Mygeocodequery_QueryCompleted;
Mygeocodequery.QueryAsync();
}
}
private void Mygeocodequery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
if (e.Error == null
&& e.Result.Count > 0)
{
MyQuery = new RouteQuery();
path.Add(e.Result[0].GeoCoordinate);
MyQuery.Waypoints = path;
MyQuery.QueryCompleted += MyQuery_QueryCompleted;
MyQuery.QueryAsync();
Mygeocodequery.Dispose();
}
}
private void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
{
if (e.Error == null)
{
Route MyRoute = e.Result;
MapRoute MyMapRoute = new MapRoute(MyRoute);
myMap.AddRoute(MyMapRoute);
MyQuery.Dispose();
path = e.Result.Geometry.ToList();
btnPlay.IsEnabled = true;
}
}
您在这里遇到了一些挑战。更新的位置将无法告诉您用户从 A 到 B 的行进路径,因此动画曲线等将很困难。
最初,我建议您将 MovementThreshold
更改为 0,这样您可以获得尽可能多的更新,从而为您提供最高的粒度,这应该可以最大限度地减少您在弯曲路径上行驶时看到的任何错误,特别是在更高的缩放级别。
关于 'old' 和 'new' 位置之间的动画,这里有一篇很棒的博客文章:
Bring your maps to life: Creating animations with Bing Maps
这涵盖了您应该能够在您的项目中使用的各种图钉动画。
我正在创建一个应用程序,当用户在他的路径上行驶时,我试图在其中移动自行车图标。我通过订阅 Geolocator class 的位置更改事件来获取用户的新位置。我每次都将图钉设置到这个新位置。但问题是图钉不会像动画那样在该路径上移动,而是突然移动到那个新位置。有什么方法可以使图钉动画以使用起点和终点沿着路径移动。这还应该包括像打开弯曲路径这样的场景。
当前代码:
XAML:
<maps:Map x:Name="myMap" ZoomLevel="16">
<toolkit:MapExtensions.Children>
<toolkit:UserLocationMarker x:Name="UserLocationMarker" />
<!--<toolkit:Pushpin x:Name="MyPushpin" Content="My Position"></toolkit:Pushpin>-->
</toolkit:MapExtensions.Children>
</maps:Map>
C#:
Geolocator geolocator = new Geolocator();
this.geolocator.MovementThreshold = 0;
RouteQuery MyQuery = null;
GeocodeQuery Mygeocodequery = null;
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
geolocator.PositionChanged += geolocator_PositionChanged;
Geoposition geoposition = await geolocator.GetGeopositionAsync();
myMap.Center = geoposition.Coordinate.ToGeoCoordinate();
}
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
UserLocationMarker marker = (UserLocationMarker)this.FindName("UserLocationMarker");
marker.GeoCoordinate = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
});
}
private void MovePinOnPath(bool isGeodesic)
{
ClearMap();
Pushpin marker = (Pushpin)this.FindName("MyPushpin");
marker.GeoCoordinate = path[0];
currentAnimation = new Animations.PathAnimation(path, (coord, pathIdx, frameIdx) =>
{
marker.GeoCoordinate = coord;
}, isGeodesic, 10000);
currentAnimation.Play();
}
private void ClearMap()
{
if (currentAnimation != null)
{
currentAnimation.Stop();
currentAnimation = null;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MovePinOnPath(true);
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrWhiteSpace(txtSearchPlace.Text))
{
Mygeocodequery = new GeocodeQuery();
Mygeocodequery.SearchTerm = txtSearchPlace.Text;
Mygeocodequery.GeoCoordinate = new GeoCoordinate(myMap.Center.Latitude, myMap.Center.Longitude);
Mygeocodequery.QueryCompleted += Mygeocodequery_QueryCompleted;
Mygeocodequery.QueryAsync();
}
}
private void Mygeocodequery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
if (e.Error == null
&& e.Result.Count > 0)
{
MyQuery = new RouteQuery();
path.Add(e.Result[0].GeoCoordinate);
MyQuery.Waypoints = path;
MyQuery.QueryCompleted += MyQuery_QueryCompleted;
MyQuery.QueryAsync();
Mygeocodequery.Dispose();
}
}
private void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
{
if (e.Error == null)
{
Route MyRoute = e.Result;
MapRoute MyMapRoute = new MapRoute(MyRoute);
myMap.AddRoute(MyMapRoute);
MyQuery.Dispose();
path = e.Result.Geometry.ToList();
btnPlay.IsEnabled = true;
}
}
您在这里遇到了一些挑战。更新的位置将无法告诉您用户从 A 到 B 的行进路径,因此动画曲线等将很困难。
最初,我建议您将 MovementThreshold
更改为 0,这样您可以获得尽可能多的更新,从而为您提供最高的粒度,这应该可以最大限度地减少您在弯曲路径上行驶时看到的任何错误,特别是在更高的缩放级别。
关于 'old' 和 'new' 位置之间的动画,这里有一篇很棒的博客文章:
Bring your maps to life: Creating animations with Bing Maps
这涵盖了您应该能够在您的项目中使用的各种图钉动画。