2011-10-06 162 views
4

上計算和繪製路線在WP7(芒果)我的應用程序我需要從一個點導航到另一個用戶。我知道有地圖控件可以讓你吸引人員,但你如何要求它爲你繪製道路? (根據指定的目標&用戶的當前位置 - 但是,這是不斷變化因此如何,如果他去的方式,你更新的路線?)在Bing地圖控制

+0

你基本上問3個問題中的一個位置。下一次,詢問*一個*詳細的問題,這樣你可以得到一個更簡潔的答案。 –

回答

15

更新與用戶當前位置的地圖,用GeoCoordinateWatcher和更新數據綁定圖釘的位置隨着它的變化而變化。請記住將最小距離設置爲低至5米。

像Bing地圖的一個圖釘,可以用這個模板XAML來創建:

<maps:Pushpin Background="{StaticResource PushpinLocationBrush}" 
       Location="{Binding MyLocation}"> 
    <maps:Pushpin.Template> 
     <ControlTemplate> 
      <Grid> 
       <Rectangle Width="15" 
          Height="15" 
          Margin="0" 
          Fill="Black"> 
        <Rectangle.Projection> 
         <PlaneProjection CenterOfRotationX="0" 
             LocalOffsetX="-2" 
             LocalOffsetY="5" 
             RotationZ="45" /> 
        </Rectangle.Projection> 
       </Rectangle> 
       <Ellipse Width="7" 
         Height="7" 
         Margin="0" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         Fill="Orange" 
         RenderTransformOrigin="0.339,0.232" 
         StrokeThickness="0" /> 
      </Grid> 
     </ControlTemplate> 
    </maps:Pushpin.Template> 
</maps:Pushpin> 

獲得一個地址會有地理座標可與Bing地圖來完成。你可以閱讀更多關於兵服務的位置:http://msdn.microsoft.com/en-us/library/cc980922.aspx - 一個你需要的是GeoCodeService

繪製路徑比較複雜,特別是如果你想讓它跟隨的道路。爲此,您需要Bing地圖路線服務。

將該服務添加到Visual Studio,並以RouteServiceReference作爲名稱,然後可以利用以下代碼獲取路徑片段,並將它們添加到您的映射中。下面的XAML反映我的片段添加到控件:

List<GeoCoordinate> locations = new List<GeoCoordinate>(); 

RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); 

routeService.CalculateRouteCompleted += (sender, e) => 
{ 
    var points = e.Result.Result.RoutePath.Points; 
    var coordinates = points.Select(x => new GeoCoordinate(x.Latitude, x.Longitude)); 

    var routeColor = Colors.Blue; 
    var routeBrush = new SolidColorBrush(routeColor); 

    var routeLine = new MapPolyline() 
    { 
     Locations = new LocationCollection(), 
     Stroke = routeBrush, 
     Opacity = 0.65, 
     StrokeThickness = 5.0, 
    }; 

    foreach (var location in points) 
    { 
     routeLine.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude)); 
    } 

    RouteLayer.Children.Add(routeLine); 
}; 

RouteBingMap.SetView(LocationRect.CreateLocationRect(locations)); 

routeService.CalculateRouteAsync(new RouteRequest() 
{ 
    Credentials = new Credentials() 
    { 
     ApplicationId = "YOURBINGMAPSKEYHERE" 
    }, 
    Options = new RouteOptions() 
    { 
     RoutePathType = RoutePathType.Points 
    }, 
    Waypoints = new ObservableCollection<Waypoint>(
     locations.Select(x => new Waypoint() 
     { 
      Location = x.Location 
     })) 
}); 

相關XAML:

<maps:Map x:Name="RouteBingMap" 
      AnimationLevel="None" 
      CopyrightVisibility="Collapsed" 
      CredentialsProvider="YOURBINGMAPSKEYHERE" 
      LogoVisibility="Collapsed" 
      ZoomBarVisibility="Collapsed" 
      ZoomLevel="12"> 
    <maps:MapLayer x:Name="RouteLayer" /> 
</maps:Map> 
+0

非常棒,thx很多 –