using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using ThinkGeo.MapSuite.SilverlightCore;
using ThinkGeo.MapSuite.SilverlightEdition;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace CSharp_Silverlight_HowDoISamples.Samples
{
public partial class RoutingDirections : UserControl
{
private SimpleMarkerOverlay markerOverlay;
private InMemoryFeatureLayer routeLayer;
public RoutingDirections()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(RoutingDirections_Loaded);
}
void RoutingDirections_Loaded(object sender, RoutedEventArgs e)
{
Map1.MapUnit = GeographyUnit.DecimalDegree;
WorldMapKitWmsSilverlightOverlay baseOverlay = new WorldMapKitWmsSilverlightOverlay();
Map1.Overlays.Add(baseOverlay);
markerOverlay = new SimpleMarkerOverlay();
Map1.Overlays.Add("SimpleMarkerOverlay", markerOverlay);
// Add the start and end flag
GetStartPointByFeatureId(startFeautreId.Text);
routeLayer = new InMemoryFeatureLayer();
routeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = new LineStyle(new GeoPen(GeoColor.FromArgb(100, GeoColor.StandardColors.Purple), 5));
routeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen.LineJoin = DrawingLineJoin.Round;
routeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen.StartCap = GeoDashCap.Round;
routeLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen.EndCap = GeoDashCap.Round;
routeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay routeOverlay = new LayerOverlay();
routeOverlay.Layers.Add("routeLayer", routeLayer);
Map1.Overlays.Add("RouteOverlay", routeOverlay);
Map1.CurrentExtent = new RectangleShape(-97.736959142883279, 30.310479724914515, -97.721080465515115, 30.299150074035609);
}
private void btnGetRoute_Click(object sender, RoutedEventArgs e)
{
GeoDataService.GeoDataGettingClient client = new GeoDataService.GeoDataGettingClient();
client.GetRouteResultCompleted += new EventHandler(client_GetRouteResultCompleted);
client.GetRouteResultAsync(startFeautreId.Text, endFeautreId.Text, String.Empty);
}
void client_GetRouteResultCompleted(object sender, CSharp_Silverlight_HowDoISamples.GeoDataService.GetRouteResultCompletedEventArgs e)
{
if (e.Error == null)
{
Dictionary> result = e.Result;
LineShape lineShape = BaseShape.CreateShapeFromWellKnownData(result["wkt"][0]) as LineShape;
routeLayer.InternalFeatures.Clear();
routeLayer.InternalFeatures.Add(new Feature(lineShape));
Map1.Refresh();
ObservableCollection results = new ObservableCollection();
ObservableCollection roadNames = result["roadName"];
ObservableCollection directions = result["direction"];
for (int i = 0; i < roadNames.Count; i++)
{
RoutingResultWithDirection DirectionResult = new RoutingResultWithDirection();
DirectionResult.Directions = directions[i];
LineShape line = ((MultilineShape)BaseShape.CreateShapeFromWellKnownData(result["roadWkts"][i])).Lines[0];
DirectionResult.Distance = line.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter).ToString("F2", CultureInfo.InvariantCulture);
DirectionResult.RoadName = result["roadName"][i];
results.Add(DirectionResult);
}
dgResult.DataContext = results;
}
}
internal void GetStartPointByFeatureId(string featureId)
{
GeoDataService.GeoDataGettingClient client = new CSharp_Silverlight_HowDoISamples.GeoDataService.GeoDataGettingClient();
client.GetPointShapeByIdCompleted += new EventHandler(client_GetStartPointShapeByIdCompleted);
client.GetPointShapeByIdAsync(featureId);
}
void client_GetStartPointShapeByIdCompleted(object sender, CSharp_Silverlight_HowDoISamples.GeoDataService.GetPointShapeByIdCompletedEventArgs e)
{
if (e.Error == null)
{
string[] positions = e.Result.Split(',');
PointShape pointShape = new PointShape(double.Parse(positions[0], CultureInfo.InvariantCulture), double.Parse(positions[1], CultureInfo.InvariantCulture));
RenderMarker(pointShape, "start");
GetEndPointByFeatureId(endFeautreId.Text);
}
}
internal void GetEndPointByFeatureId(string featureId)
{
GeoDataService.GeoDataGettingClient client = new CSharp_Silverlight_HowDoISamples.GeoDataService.GeoDataGettingClient();
client.GetPointShapeByIdCompleted += new EventHandler(client_GetEndPointShapeByIdCompleted);
client.GetPointShapeByIdAsync(featureId);
}
void client_GetEndPointShapeByIdCompleted(object sender, CSharp_Silverlight_HowDoISamples.GeoDataService.GetPointShapeByIdCompletedEventArgs e)
{
if (e.Error == null)
{
string[] positions = e.Result.Split(',');
PointShape pointShape = new PointShape(double.Parse(positions[0], CultureInfo.InvariantCulture), double.Parse(positions[1], CultureInfo.InvariantCulture));
RenderMarker(pointShape, "end");
}
}
private void RenderMarker(PointShape pointShape, string renderTag)
{
Marker marker = new Marker(pointShape);
if (renderTag == "start")
{
marker.ImageSource = new BitmapImage(new Uri("/theme/StartpointMarker.png", UriKind.RelativeOrAbsolute));
}
else if (renderTag == "end")
{
marker.ImageSource = new BitmapImage(new Uri("/theme/EndpointMarker.png", UriKind.RelativeOrAbsolute));
}
marker.ImageOffsetY = -10;
markerOverlay.Markers.Add(marker);
Map1.Refresh();
}
}
public class RoutingResultWithDirection
{
public string RoadName { get; set; }
public string Distance { get; set; }
public string Directions { get; set; }
}
}