Windows Phone使图钉图标在开始和结束位置之间移动

时间:2015-05-14 09:25:28

标签: c# wpf silverlight windows-phone-8 dictionary

我正在创建一个应用程序,我试图在用户旅行的路径上移动自行车图标。我通过订阅Geolocator类的位置更改事件来获取用户的新位置。我每次都将图钉设置到这个新位置。但问题是图钉不会像动画一样在那条路上移动,而是突然转向新的位置。有没有办法使用起点和终点动画图钉沿路径移动。这还应该包括像打开弯曲路径这样的场景。

当前代码:

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;
            }
        }

1 个答案:

答案 0 :(得分:1)

这里有几个挑战。更新后的位置将无法告诉您用户从A行进到B的路径,因此动画曲线等将很困难。

最初,我建议您将MovementThreshold更改为0,以便获得尽可能多的更新,以便为您提供最高的粒度,这可以最大限度地减少您在弯道上行进时会出现的任何错误,尤其是在更高的缩放级别。

关于“旧”和“新”位置之间的动画,这里有一篇很棒的博客文章:

Bring your maps to life: Creating animations with Bing Maps

这涵盖了您应该能够在项目中使用的各种图钉动画。