如何在windows phone 8地图控件中的两个特定位置之间画一条路线?

时间:2013-03-06 18:25:17

标签: windows-phone-8

我看到了这段代码,但只有在搜索指定目的地时才有效! 我需要使用纬度和经度导航到特定位置。

GeocodeQuery Mygeocodequery = null;
Mygeocodequery = new GeocodeQuery();
Mygeocodequery.SearchTerm = "Seattle, WA";
Mygeocodequery.GeoCoordinate = new GeoCoordinate(MyGeoPosition.Coordinate.Latitude,MyGeoPosition.Coordinate.Longitude);
Mygeocodequery.QueryCompleted += Mygeocodequery_QueryCompleted;
Mygeocodequery.QueryAsync();
void Mygeocodequery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
if (e.Error == null)
 {
  MyQuery = new RouteQuery();
  MyCoordinates.Add(e.Result[0].GeoCoordinate);
  MyQuery.Waypoints = MyCoordinates;
  MyQuery.QueryCompleted += MyQuery_QueryCompleted;
  MyQuery.QueryAsync();
  Mygeocodequery.Dispose();
 }
}
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();
}
}

1 个答案:

答案 0 :(得分:1)

这是一个示例代码(假设您已有源和目标坐标)。

    public partial class Foo : PhoneApplicationPage
    {
        List<GeoCoordinate> MyCoordinates = new List<GeoCoordinate>();

        public Foo()
        {
             InitializeComponent();
        }

        private void SetMapCoordinates()
        {
            MyCoordinates.Add(new GeoCoordinate(Target.Latitude, Target.Longitude));
            MyCoordinates.Add(new GeoCoordinate(Source.Latitude, Source.Longitude));
            DrawMyRoute();
        }

        void MyRoute()
        {
            MyQuery = new RouteQuery();
            MyQuery.TravelMode = TravelMode.Walking;
            MyQuery.Waypoints = MyCoordinates;
            MyQuery.QueryCompleted += MyQuery_QueryCompleted;
            MyQuery.QueryAsync();
        }

        void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
        {
            if (e.Error == null)
            {
                Route MyRoute = e.Result;
                MapRoute MyMapRoute = new MapRoute(MyRoute);
                MyMapRoute.Color = (Colors.Blue);
                MyLocMap.AddRoute(MyMapRoute);
                #region Draw source location ellipse
                    Ellipse myCircle = new Ellipse();
                    myCircle.Fill = new SolidColorBrush(Colors.Blue);
                    myCircle.Height = 20;
                    myCircle.Width = 20;
                    myCircle.Opacity = 50;
                    MapOverlay myLocationOverlay = new MapOverlay();
                    myLocationOverlay.Content = myCircle;
                    myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                    myLocationOverlay.GeoCoordinate = CoordinateConverter.ConvertGeocoordinate(SourceLocation);
                    MylocationLayer = new MapLayer();
                    MylocationLayer.Add(myLocationOverlay);
                    MyLocMap.Layers.Add(MylocationLayer);
            #endregion
            #region Draw target location ellipse
                    Ellipse CarCircle = new Ellipse();
                    CarCircle.Fill = new SolidColorBrush(Colors.Red);
                    CarCircle.Height = 20;
                    CarCircle.Width = 20;
                    CarCircle.Opacity = 50;
                    MapOverlay CarLocationOverlay = new MapOverlay();
                    CarLocationOverlay.Content = CarCircle;
                    CarLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                    CarLocationOverlay.GeoCoordinate = CoordinateConverter.ConvertGeocoordinate(TargetLocation);
                    CarlocationLayer = new MapLayer();
                    CarlocationLayer.Add(CarLocationOverlay);
                    MyLocMap.Layers.Add(CarlocationLayer);
            #endregion
            MyQuery.Dispose();
           }
       }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SetMapCoordinates();
        }
    }