2013-03-06 49 views
1

我看到了这段代码,但它只适用于目的地是由搜索指定的! 我需要使用纬度和经度导航到特定的位置。如何在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

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

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(); 
     } 
    } 
相关问题