Select to view content in your preferred language

Problem with route path (please help)

660
1
06-26-2012 01:22 PM
AbdulrahmanTaher
Emerging Contributor
Hello every one

I used the sample of bing map route , and try to customize on my computer , but with out grid of bing key , i was enter it in the tiled layer . Every thing done well , but when i click Route Button , the path doesn't view
as well as description . Please any one can show me where the problem in the code. This is the code

XAML
----

<UserControl x:Class="Silverlight_BingRouteService.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="467" d:DesignWidth="655"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
   xmlns:bing="clr-namespace:ESRI.ArcGIS.Client.Bing;assembly=ESRI.ArcGIS.Client.Bing" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.Resources>
            <ControlTemplate x:Key="CompositeSymbol">
                <Grid>
                    <Ellipse Fill="{Binding Symbol.Color}" Width="{Binding Symbol.Size}" Height="{Binding Symbol.Size}" Stroke="Black" StrokeThickness="1" />
                    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                  Text="{Binding Path=Attributes[StopNumber]}"
                  FontSize="9" Margin="0" FontWeight="Bold" Foreground="Black" />
                </Grid>
            </ControlTemplate>
            <esri:SimpleMarkerSymbol x:Key="UserStopSymbol" Size="20" Style="Circle" Color="Salmon" ControlTemplate="{StaticResource CompositeSymbol}" />
            <esri:SimpleMarkerSymbol x:Key="ResultStopSymbol" Size="20" Color="GreenYellow" ControlTemplate="{StaticResource CompositeSymbol}" />
            <esri:SimpleLineSymbol x:Key="RoutePathSymbol" Style="Solid" Width="4" Color="YellowGreen" />
        </Grid.Resources>


        <esri:Map Name="MyMap"  Margin="12,48,256,92">
            <esri:Map.Layers>
                <bing:TileLayer ID="BingLayerRoad" LayerStyle="Road" ServerType="Production"
                                Token="put here My Token" />
                <esri:GraphicsLayer ID="RouteResultsGraphicsLayer" />
                <esri:GraphicsLayer ID="WaypointGraphicsLayer" />
    
            </esri:Map.Layers>
        </esri:Map>

        <StackPanel Name="RouteGrid" HorizontalAlignment="Right" VerticalAlignment="Top" MaxWidth="250" Visibility="Visible">
            <Grid VerticalAlignment="Top" Margin="10" >
                <Rectangle Fill="#FF919191" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="0" >
                    <Rectangle.Effect>
                        <DropShadowEffect/>
                    </Rectangle.Effect>
                </Rectangle>
                <StackPanel Margin="10">
                    <TextBlock Text="Click on map to define waypoints. When finished, click the Solve Route button to generate a route."
                           TextWrapping="Wrap" Foreground="White" Width="225" />
                    <Button x:Name="RouteButton" Content="Solve Route" Margin="2" Width="100" Height="20" HorizontalAlignment="Center"
                            Click="RouteButton_Click" />
                    <Button x:Name="ClearRouteButton" Content="Clear Route" Margin="2" Width="100" Height="20" HorizontalAlignment="Center"
                            Click="ClearRouteButton_Click" />
                </StackPanel>
            </Grid>
            <Grid x:Name="DirectionsGrid" VerticalAlignment="Top" Margin="10" Visibility="Visible" >
                <Rectangle Fill="#FF919191" Stroke="Gray"  RadiusX="10" RadiusY="10" Margin="0" >
                    <Rectangle.Effect>
                        <DropShadowEffect/>
                    </Rectangle.Effect>
                </Rectangle>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="5" >
                    <TextBlock x:Name="DirectionsTitleTextBlock" Margin="5,0,0,5" Text="Directions" FontSize="12" Foreground="White" FontWeight="Bold" />
                    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" MaxWidth="250" MaxHeight="300" >
                        <TextBlock x:Name="DirectionsContentTextBlock" Foreground="White" />
                    </ScrollViewer>
                </StackPanel>
            </Grid>
        </StackPanel>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="41,400,0,0" Name="textBlock1" Text="AovK5oAWwtU-wAbePD4EULZAEiImElaRJc5cTBcSwru0DuH6LYs8g4SpKPL4eJ2P" VerticalAlignment="Top" Visibility="Collapsed" />
    </Grid>
</UserControl>



   

0 Kudos
1 Reply
AbdulrahmanTaher
Emerging Contributor
Code Behind
---


namespace Silverlight_BingRouteService
{
    public partial class MainPage : UserControl
    {

        private Draw myDrawObject;
        private GraphicsLayer waypointGraphicsLayer;
        private GraphicsLayer routeResultsGraphicsLayer;
        private ESRI.ArcGIS.Client.Bing.Routing routing;
        private static ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();

        public MainPage()
        {
            InitializeComponent();


            routing = new ESRI.ArcGIS.Client.Bing.Routing(Application.Current.Resources["My Token"] as string);
            routing.ServerType = ServerType.Production;

            myDrawObject = new Draw(MyMap)
            {
                DrawMode = DrawMode.Point,
                IsEnabled = true
            };

            myDrawObject.DrawComplete += MyDrawObject_DrawComplete;
            waypointGraphicsLayer = MyMap.Layers["WaypointGraphicsLayer"] as GraphicsLayer;
            routeResultsGraphicsLayer = MyMap.Layers["RouteResultsGraphicsLayer"] as GraphicsLayer;

            ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
                            new ESRI.ArcGIS.Client.Geometry.Envelope(
                    mercator.FromGeographic(
                            new ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)) as MapPoint,
                    mercator.FromGeographic(
                            new ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)) as MapPoint);

            initialExtent.SpatialReference = new SpatialReference(102100);

            MyMap.Extent = initialExtent;

               
        }

        private void MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {
            myDrawObject.IsEnabled = false;

            Graphic waypointGraphic = new Graphic()
            {   //// declare esri.....geometry.ge
                Geometry = MyMap.WrapAroundIsActive ? Geometry.NormalizeCentralMeridian(args.Geometry) : args.Geometry,
                Symbol = LayoutRoot.Resources["UserStopSymbol"] as Symbol
            };

            waypointGraphic.Attributes.Add("StopNumber", waypointGraphicsLayer.Graphics.Count + 1);
            waypointGraphicsLayer.Graphics.Add(waypointGraphic);

            if (waypointGraphicsLayer.Graphics.Count > 1)
                RouteButton.IsEnabled = true;
            myDrawObject.IsEnabled = true;
        }
      
        private void RouteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            myDrawObject.IsEnabled = false;
            routing.Optimization = RouteOptimization.MinimizeTime;
            routing.TrafficUsage = TrafficUsage.None;
            routing.TravelMode = TravelMode.Driving;
            routing.Route(GraphicsToMapPoints(), Route_Complete);
        }

      
        private List<MapPoint> GraphicsToMapPoints()
        {
            List<MapPoint> mapPoints = new List<MapPoint>();
            foreach (Graphic g in waypointGraphicsLayer.Graphics)
                mapPoints.Add(g.Geometry as MapPoint);

            return mapPoints;
        }

       
        private void Route_Complete(object sender, CalculateRouteCompletedEventArgs args)
        {
            myDrawObject.IsEnabled = true;
            //routeResultsGraphicsLayer.ClearGraphics();
            //waypointGraphicsLayer.ClearGraphics();

            StringBuilder directions = new StringBuilder();

            ObservableCollection<RouteLeg> routeLegs = args.Result.Result.Legs;
            int numLegs = routeLegs.Count;
            int instructionCount = 0;
            for (int n = 0; n < numLegs; n++)
            {
                if ((n % 2) == 0)
                {
                    AddStopPoint(mercator.FromGeographic(new MapPoint(routeLegs.ActualStart.Longitude, routeLegs.ActualStart.Latitude)) as MapPoint);
                    AddStopPoint(mercator.FromGeographic(new MapPoint(routeLegs.ActualEnd.Longitude, routeLegs.ActualEnd.Latitude)) as MapPoint);
                }
                else if (n == (numLegs - 1))
                {
                    AddStopPoint(mercator.FromGeographic(new MapPoint(routeLegs.ActualEnd.Longitude, routeLegs.ActualEnd.Latitude)) as MapPoint);
                }

                directions.Append(string.Format("--Leg #{0}--\n", n + 1));

                foreach (ItineraryItem item in routeLegs.Itinerary)
                {
                    instructionCount++;
                    directions.Append(string.Format("{0}. {1}\n", instructionCount, item.Text));
                }
            }

            Regex regex = new Regex("<[/a-zA-Z:]*>",
                    RegexOptions.IgnoreCase | RegexOptions.Multiline);

            DirectionsContentTextBlock.Text = regex.Replace(directions.ToString(), string.Empty);
            DirectionsGrid.Visibility = Visibility.Visible;

            RoutePath routePath = args.Result.Result.RoutePath;

            Polyline line = new Polyline();
            line.Paths.Add(new PointCollection());

            foreach (ESRI.ArcGIS.Client.Bing.RouteService.Location location in routePath.Points)
                line.Paths[0].Add(mercator.FromGeographic(new MapPoint(location.Longitude, location.Latitude)) as MapPoint);

            Graphic graphic = new Graphic()
            {
                Geometry = line,
                Symbol = LayoutRoot.Resources["RoutePathSymbol"] as Symbol
            };
            routeResultsGraphicsLayer.Graphics.Add(graphic);
        }

        /
        private void AddStopPoint(MapPoint mapPoint)
        {
            Graphic graphic = new Graphic()
            {
                Geometry = mapPoint,
                Symbol = LayoutRoot.Resources["ResultStopSymbol"] as Symbol
            };
            graphic.Attributes.Add("StopNumber", waypointGraphicsLayer.Graphics.Count + 1);
            waypointGraphicsLayer.Graphics.Add(graphic);
        }

        private void ClearRouteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            waypointGraphicsLayer.ClearGraphics();
            routeResultsGraphicsLayer.ClearGraphics();
            DirectionsContentTextBlock.Text = "";
            DirectionsGrid.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
 
   
Thanks alot
0 Kudos