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