/*/public void CalcDima(RootObject daveRequestJson)
         * {
         *  List<xDimaWSService.RouteLocation> dimaWayPointList = new List<xDimaWSService.RouteLocation>();
         *  xDimaWSService.RouteLocation[] dipw = null;
         *
         *  var violatedLocationsList = new List<xDimaWSService.OnRoadRouteLocation>();
         *  violatedIdList = new List<string>();
         *  idList = new List<string>();
         *
         *  var drr = new xDimaWSService.CreateAndGetDistanceMatrixRequest
         *  {
         *      coordinateFormat = "EPSG:4326",
         *      storedProfile = cbxStoredProfile.Text
         *  };
         *
         *  var dimaTaTheme = new xDimaWSService.Theme
         *  {
         *      id = "PTV_TruckAttributes",
         *      enabledSpecified = true
         *  };
         *
         *  dimaTaTheme.enabled = false;
         *
         *  if (cbxFLTruckAttributes.Checked == true)
         *  {
         *      dimaTaTheme.enabled = true;
         *  }
         *
         *  drr.requestProfile = new xDimaWSService.RequestProfile()
         *  {
         *      featureLayerProfile = new xDimaWSService.FeatureLayerProfile()
         *      {
         *          themes = new xDimaWSService.Theme[]
         *          {
         *              dimaTaTheme
         *          },
         *      },
         *  };
         *
         *  if (daveRequestJson.stops.Count >= 2)
         *  {
         *      foreach (var stop in daveRequestJson.stops)
         *      {
         *          var wpt = new xDimaWSService.OnRoadRouteLocation()
         *          {
         *              coordinate = new xDimaWSService.Coordinate()
         *              {
         *                  x = stop.coordinate.locationX,
         *                  y = stop.coordinate.locationY
         *              }
         *          };
         *          dimaWayPointList.Add(wpt);
         *      }
         *  }
         *  else
         *  {
         *      MessageBox.Show("Not enough waypoints!");
         *      return;
         *  }
         *
         *  foreach (var scemId in daveRequestJson.stops)
         *  {
         *      idList.Add(scemId.scemid.ToString());
         *  }
         *
         *  dipw = dimaWayPointList.ToArray();
         *  drr.startLocations = dipw;
         *  var locationsList = dipw.OfType<xDimaWSService.OnRoadRouteLocation>().ToList();
         *  drr.resultFields = new xDimaWSService.DistanceMatrixContentsResultFields()
         *  {
         *      distances = true,
         *      distancesSpecified = true,
         *      violated = true,
         *      violatedSpecified = true,
         *      estimatedByDirectDistance = true,
         *      estimatedByDirectDistanceSpecified = true
         *  };
         *
         *  try
         *  {
         *      dimaResponse = xDimaClient.createDistanceMatrix(drr);
         *  }
         *  catch (System.Web.Services.Protocols.SoapException soapException)
         *  {
         *      System.Windows.Forms.MessageBox.Show(soapException.Detail.InnerXml.Replace(">", ">\r\n"));
         *      return;
         *  }
         *  catch (System.ServiceModel.FaultException faultException)
         *  {
         *      System.Windows.Forms.MessageBox.Show("Error when trying to calculate the dima" + "\r\n" + faultException.Message);
         *      return;
         *  }
         *  catch (Exception ex)
         *  {
         *      MessageBox.Show(ex.Message);
         *      return;
         *  }
         *
         *  waypointCount = dimaResponse.summary.numberOfStartLocations;
         *  distanceMatrixResponseContents = dimaResponse.contents as xDimaWSService.DistanceMatrixContentsArrays;
         *
         *  for (int i = 0; i < dimaResponse.summary.numberOfStartLocations; i++)
         *  {
         *      var numberOfViolations = 0;
         *      for (int j = 0; j < dimaResponse.summary.numberOfStartLocations; j++)
         *      {
         *          if (distanceMatrixResponseContents.violated[i * dimaResponse.summary.numberOfStartLocations + j])
         *          {
         *              numberOfViolations++;
         *          }
         *      }
         *      if (numberOfViolations >= waypointCount - 1)
         *      {
         *          violatedLocationsList.Add(locationsList[i]);
         *      }
         *  }
         *
         *  ShowDimaInDynMap(idList, locationsList, violatedLocationsList);
         *  waypointCount = 0;
         * }
         * /*/

        public void ShowRouteInDynMap()
        {
            var mapCoordFormat = "";

            if (cbxCoordFormat.Text == "EPSG:76131")
            {
                routingLayer = new ShapeLayer("Route")
                {
                    SpatialReferenceId = "PTV_MERCATOR"
                };
                mapCoordFormat = "PTV_MERCATOR";
            }
            else if (cbxCoordFormat.Text == "EPSG:4326")
            {
                routingLayer = new ShapeLayer("Route")
                {
                    SpatialReferenceId = "EPSG:4326"
                };
                mapCoordFormat = "EPSG:4326";
            }
            else if (cbxCoordFormat.Text == "EPSG:3857")
            {
                routingLayer = new ShapeLayer("Route")
                {
                    SpatialReferenceId = "EPSG:3857"
                };
                mapCoordFormat = "EPSG:3857";
            }

            if (routeResponse.polyline != null)
            {
                var routePoly = routeResponse.polyline.plain as xRouteWSService.Polyline;

                var points = new PointCollection();

                foreach (xRouteWSService.Coordinate p in routePoly.polyline)
                {
                    points.Add(new System.Windows.Point(p.x, p.y));
                }

                MapPolyline mapPolyLine = new MapPolyline()
                {
                    Points             = points,
                    Stroke             = new SolidColorBrush(Colors.Blue),
                    MapStrokeThickness = 10,
                    Opacity            = 1.0,
                    ScaleFactor        = 0.2
                };

                (routingLayer as ShapeLayer).Shapes.Add(mapPolyLine);
                mapPolyLine.ToolTip = string.Format("{0:0,0.0}km\n{1}", Math.Round(routeResponse.distance / 1000.0, 2), TimeSpan.FromSeconds(routeResponse.travelTime));
            }
            else
            {
                MessageBox.Show("Error while calculating route");
                return;
            }

            var markerStart = new Ptv.XServer.Controls.Map.Symbols.Ball
            {
                Color = Colors.Green
            };
            var markerVia = new Ptv.XServer.Controls.Map.Symbols.Ball
            {
                Color = Colors.Yellow
            };
            var markerDest = new Ptv.XServer.Controls.Map.Symbols.Ball
            {
                Color = Colors.Red
            };

            mapCoordStart = new System.Windows.Point(double.Parse(tbxStartX.Text.Replace(".", ",")), double.Parse(tbxStartY.Text.Replace(".", ",")));
            mapCoordDest  = new System.Windows.Point(double.Parse(tbxDestX.Text.Replace(".", ",")), double.Parse(tbxDestY.Text.Replace(".", ",")));
            ShapeCanvas.SetLocation(markerStart, mapCoordStart);
            ShapeCanvas.SetLocation(markerDest, mapCoordDest);
            markerStart.Width   = markerStart.Height = markerDest.Height = markerDest.Width = markerVia.Height = markerVia.Width = 10;
            markerStart.ToolTip = "Start";
            markerDest.ToolTip  = "Destination";

            if (cbxManipulateWpt.Checked == true)
            {
                mapCoordVia = new System.Windows.Point(double.Parse(tbxViaX.Text.Replace(".", ",")), double.Parse(tbxViaY.Text.Replace(".", ",")));
                ShapeCanvas.SetLocation(markerVia, mapCoordVia);
                markerVia.ToolTip = "Via";
                routingLayer.Shapes.Add(markerVia);
            }

            routingLayer.Shapes.Add(markerStart);
            routingLayer.Shapes.Add(markerDest);

            dynMap.Layers.Add(routingLayer);

            //zoom to contain route
            MapRectangle mergedRoutesRectangle = new MapRectangle();
            var          polyLine = routeResponse.polyline.plain as xRouteWSService.Polyline;

            var winPoints = from coord in polyLine.polyline
                            select new System.Windows.Point(coord.x, coord.y);

            mergedRoutesRectangle |= new MapRectangle(winPoints);

            dynMap.SetEnvelope(mergedRoutesRectangle.Inflate(1.2), mapCoordFormat);

            routingLayer.Refresh();
        }