Tag Archives: REST

Using the Bing Maps REST Services To Obtain the Route in the .NET Application

The Bing Maps platform provides multiple API options for your application including an AJAX control, a Windows Store apps control, a WPF control, REST Services, and Spatial Data Services. See more information: http://www.microsoft.com/maps/choose-your-bing-maps-API.aspx

To obtain the route data, Bing Maps REST Services is our choice: http://msdn.microsoft.com/en-us/library/ff701713.aspx

The Bing™ Maps REST Services Application Programming Interface (API) provides a Representational State Transfer (REST) interface to perform tasks such as creating a static map with pushpins, geocoding an address, retrieving imagery metadata, or creating a route.

About using the REST Services with .NET, this article is helpful: http://msdn.microsoft.com/en-us/library/jj819168.aspx

Here is my WPF sample:

XAML:

<Grid>
        <m:Map Name="myMap" 
               Center="-1.968404, 30.036240" 
               ZoomLevel="10"
               CredentialsProvider="Fill Your Key"
               Mode="AerialWithLabels" ></m:Map>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Label Content="From: " FontWeight="Bold" />
            <TextBox Width="80" Height="20" Name="fromTxb" Text="New York" />
            <Label Content="To: " FontWeight="Bold" />
            <TextBox Width="80" Height="20" Name="toTxb" Text="Seattle" />
            <Button Content="Route" Width="80" Click="RouteButton_Click" />
        </StackPanel>
    </Grid>

Using the following method to request route data from REST Services:

private void Route(string start, string end, string key, Action<Response> callback)
{
            //Culture c = CultureDD.SelectedItem as Culture;
            Uri requestURI = new Uri(string.Format("http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0={0}&wp.1={1}&rpo=Points&key={2}", Uri.EscapeDataString(start), Uri.EscapeDataString(end), key));
            //QueryURLTbx.Text = requestURI.AbsoluteUri;
            GetResponse(requestURI, callback);
}

private void GetResponse(Uri uri, Action<Response> callback)
{
            try
            {
                HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));

                        if (callback != null)
                        {
                            callback(ser.ReadObject(stream) as Response);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
}

Getting the credential:

private void GetRoute(string fromloc, string toloc)
{
            string to = toloc;//"Seattle";
            string from = fromloc;//"New York";

            if (!string.IsNullOrWhiteSpace(from))
            {
                if (!string.IsNullOrWhiteSpace(to))
                {
                    GetKey((c) =>
                    {
                        Route(from, to, c, (r) =>
                        {
                            if (r != null &&
                                r.ResourceSets != null &&
                                r.ResourceSets.Length > 0 &&
                                r.ResourceSets[0].Resources != null &&
                                r.ResourceSets[0].Resources.Length > 0)
                            {
                                Route route = r.ResourceSets[0].Resources[0] as Route;

                                double[][] routePath = route.RoutePath.Line.Coordinates;
                                LocationCollection locs = new LocationCollection();

                                for (int i = 0; i < routePath.Length; i++)
                                {
                                    if (routePath[i].Length >= 2)
                                    {
                                        locs.Add(new Microsoft.Maps.MapControl.WPF.Location(routePath[i][0], routePath[i][1]));
                                    }
                                }

                                MapPolyline routeLine = new MapPolyline()
                                {
                                    Locations = locs,
                                    Stroke = new SolidColorBrush(Colors.Red),
                                    StrokeThickness = 5
                                };

                                myMap.Children.Add(routeLine);

                                myMap.SetView(locs, new Thickness(30), 0);
                            }
                            else
                            {
                                MessageBox.Show("No Results found.");
                            }
                        });
                    });
                }
                else
                {
                    MessageBox.Show("Invalid End location.");
                }
            }
            else
            {
                MessageBox.Show("Invalid Start location.");
            }
}

Screenshot(gif):
WpfBingMapsRoute

Download Sample: WpfBingMapsRoute

To get the Bing Maps Trial Key, please go to here: http://www.microsoft.com/maps/

Here are some Bing Maps resources, please refer to: Bing Maps Resources