/// <summary>
        /// Generates a route REST request
        /// </summary>
        /// <param name="startIdx"></param>
        /// <param name="endIdx"></param>
        /// <returns></returns>
        private string GetRequestUrl(int startIdx, out int endIdx)
        {
            endIdx = Waypoints.Count;

            var sb = new StringBuilder(this.Domain);

            var TravelMode = (RouteOptions != null) ? RouteOptions.TravelMode : TravelModeType.Driving;

            sb.AppendFormat("Routes/{0}?", Enum.GetName(typeof(TravelModeType), TravelMode));

            if (TravelMode != TravelModeType.Truck)
            {
                int wayCnt = 0, viaCnt = 0;

                for (int i = startIdx; i < Waypoints.Count; i++)
                {
                    if (Waypoints[i].IsViaPoint)
                    {
                        sb.AppendFormat("&vwp.{0}=", i - startIdx);
                        viaCnt++;

                        if (TravelMode == TravelModeType.Transit)
                        {
                            throw new Exception("ViaWaypoints not supported for Transit directions.");
                        }
                    }
                    else
                    {
                        sb.AppendFormat("&wp.{0}=", i - startIdx);

                        if (viaCnt > 10)
                        {
                            throw new Exception("More than 10 viaWaypoints between waypoints.");
                        }

                        wayCnt++;
                        viaCnt = 0;
                    }

                    if (Waypoints[i].Coordinate != null)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "{0:0.#####},{1:0.#####}", Waypoints[i].Coordinate.Latitude, Waypoints[i].Coordinate.Longitude);
                    }
                    else
                    {
                        sb.AppendFormat("{0}", Uri.EscapeDataString(Waypoints[i].Address));
                    }

                    //Only allow up to the batchSize waypoints in a request.
                    if (wayCnt == batchSize)
                    {
                        endIdx = i;
                        break;
                    }
                }

                if (RouteOptions != null)
                {
                    sb.Append(RouteOptions.GetUrlParam(startIdx));
                }
            }

            sb.Append(GetBaseRequestUrl());

            return(sb.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Gets a URL for requesting imagery data for a POST request.
        /// </summary>
        /// <returns>Imagery request URL for POST request.</returns>
        public string GetPostRequestUrl()
        {
            var isQuery = !string.IsNullOrWhiteSpace(Query);
            var isRoute = (Waypoints != null && Waypoints.Count >= 2);

            var sb = new StringBuilder(this.Domain);

            sb.Append("Imagery/Map/");

            sb.Append(Enum.GetName(typeof(ImageryType), ImagerySet));

            if (CenterPoint != null)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "/{0:0.#####},{1:0.#####}/", CenterPoint.Latitude, CenterPoint.Longitude);

                if (zoomLevel != 0)
                {
                    sb.Append(zoomLevel);
                }
                else if (HighlightEntity)
                {
                    sb.Append(Enum.GetName(typeof(EntityType), EntityType));
                }
                else if (ImagerySet == ImageryType.Road || ImagerySet == ImageryType.Aerial || ImagerySet == ImageryType.AerialWithLabels ||
                         ImagerySet == ImageryType.RoadOnDemand || ImagerySet == ImageryType.AerialWithLabelsOnDemand || ImagerySet == ImageryType.CanvasDark ||
                         ImagerySet == ImageryType.CanvasGray || ImagerySet == ImageryType.CanvasLight)
                {
                    throw new Exception("Zoom Level must be specified when a centerPoint is specified and ImagerySet is Road, Aerial, AerialWithLabels, or any variation of these (Canvas/OnDemand).");
                }
            }
            else if (isQuery)
            {
                sb.AppendFormat("/{0}", Uri.EscapeDataString(Query));
            }

            if (isRoute)
            {
                sb.AppendFormat("/Routes/{0}?", Enum.GetName(typeof(TravelModeType), (RouteOptions != null) ? RouteOptions.TravelMode : TravelModeType.Driving));
            }
            else
            {
                sb.Append("?");
            }

            sb.AppendFormat("ms={0},{1}", mapWidth, mapHeight);

            if (DeclutterPins)
            {
                sb.Append("&dcl=1");
            }

            if (Format.HasValue)
            {
                sb.AppendFormat("&fmt={0}", Enum.GetName(typeof(ImageFormatType), Format.Value));
            }

            if (MapArea != null && (CenterPoint == null || !isRoute))
            {
                sb.AppendFormat("&ma={0}", MapArea.ToString());
            }

            if (ShowTraffic)
            {
                sb.Append("&ml=TrafficFlow");
            }

            if (GetMetadata)
            {
                sb.Append("&mmd=1");
            }

            if (HighlightEntity)
            {
                sb.Append("&he=1");
            }

            if (Resolution == ImageResolutionType.High)
            {
                sb.Append("&dpi=Large");
            }

            //Routing Parameters

            if (isRoute)
            {
                if (Waypoints.Count > 25)
                {
                    throw new Exception("More than 25 waypoints in route request.");
                }

                for (int i = 0; i < Waypoints.Count; i++)
                {
                    sb.AppendFormat("&wp.{0}=", i);

                    if (Waypoints[i].Coordinate != null)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "{0:0.#####},{1:0.#####}", Waypoints[i].Coordinate.Latitude, Waypoints[i].Coordinate.Longitude);
                    }
                    else
                    {
                        sb.AppendFormat("{0}", Uri.EscapeDataString(Waypoints[i].Address));
                    }
                }

                if (RouteOptions != null)
                {
                    sb.Append(RouteOptions.GetUrlParam(0));
                }
            }

            sb.Append(GetBaseRequestUrl());

            if (!string.IsNullOrWhiteSpace(Style))
            {
                var s = CustomMapStyleManager.GetRestStyle(Style);

                if (!string.IsNullOrWhiteSpace(s))
                {
                    sb.AppendFormat("&st={0}", s);
                }
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Gets a URL for requesting imagery data for a POST request.
        /// </summary>
        /// <returns>Imagery request URL for POST request.</returns>
        public string GetPostRequestUrl()
        {
            var isQuery = !string.IsNullOrWhiteSpace(Query);
            var isRoute = (Waypoints != null && Waypoints.Count >= 2);

            var sb = new StringBuilder();

            sb.Append("https://dev.virtualearth.net/REST/v1/Imagery/Map/");

            sb.Append(Enum.GetName(typeof(ImageryType), ImagerySet));

            if (CenterPoint != null)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "/{0:0.#####},{1:0.#####}/", CenterPoint.Latitude, CenterPoint.Longitude);

                if (zoomLevel != 0)
                {
                    sb.Append(zoomLevel);
                }
                else if (HighlightEntity)
                {
                    sb.Append(Enum.GetName(typeof(EntityType), EntityType));
                }
                else if (ImagerySet == ImageryType.Road || ImagerySet == ImageryType.Aerial || ImagerySet == ImageryType.AerialWithLabels)
                {
                    throw new Exception("A zoom level or Entity type must be specified when when a center point is specified.");
                }
            }
            else if (isQuery)
            {
                sb.AppendFormat("/{0}", Uri.EscapeDataString(Query));
            }

            if (isRoute)
            {
                sb.AppendFormat("/Routes/{0}?", Enum.GetName(typeof(TravelModeType), (RouteOptions != null) ? RouteOptions.TravelMode : TravelModeType.Driving));
            }
            else
            {
                sb.Append("?");
            }

            sb.AppendFormat("ms={0},{1}", mapWidth, mapHeight);

            if (DeclutterPins)
            {
                sb.Append("&dcl=1");
            }

            if (Format.HasValue)
            {
                sb.AppendFormat("&fmt={0}", Enum.GetName(typeof(ImageFormatType), Format.Value));
            }

            if (MapArea != null && (CenterPoint == null || !isRoute))
            {
                sb.AppendFormat("&ma={0}", MapArea.ToString());
            }

            if (ShowTraffic)
            {
                sb.Append("&ml=TrafficFlow");
            }

            if (GetMetadata)
            {
                sb.Append("&mmd=1");
            }

            if (HighlightEntity)
            {
                sb.Append("&he=1");
            }

            if (Resolution == ImageResolutionType.High)
            {
                sb.Append("&dpi=Large");
            }

            //Routing Parameters

            if (isRoute)
            {
                if (Waypoints.Count > 25)
                {
                    throw new Exception("More than 25 waypoints in route request.");
                }

                for (int i = 0; i < Waypoints.Count; i++)
                {
                    sb.AppendFormat("&wp.{0}=", i);

                    if (Waypoints[i].Coordinate != null)
                    {
                        sb.AppendFormat("{0:0.#####},{1:0.#####}", Waypoints[i].Coordinate.Latitude, Waypoints[i].Coordinate.Longitude);
                    }
                    else
                    {
                        sb.AppendFormat("{0}", Uri.EscapeDataString(Waypoints[i].Address));
                    }
                }

                if (RouteOptions != null)
                {
                    sb.Append(RouteOptions.GetUrlParam());
                }
            }

            sb.Append(GetBaseRequestUrl());

            return(sb.ToString());
        }