none
Bing Maps API Distance Calculation

    Question

  • Hi there, I have a question regarding a C#.NET Project in which I want to use the Bing Maps API to calculate the distance between two coordinates.


    What I did in Detail:

    • Registered for bing with a Windows Live ID
    • confirmed the registration code
    • logged in
    • registered a website under "My Sites"
    • uploaded the BingAuth.xml on my Site
    • confirmed the possession of the site
    • Went to "Webmasters API" menu point (https://ssl.bing.com/webmaster/home/api?url=my_site)
    • generated a API key
    • Started a Visual Studio 2012 Windows Forms Project
    • created a Service Reference for
    http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc?wsdl


    gave it the Namespace name "BingMaps"

    • Here is my App.config-File which lies in the Main Section of the Project Explorer
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IRouteService" />
                </basicHttpBinding>
                <customBinding>
                    <binding name="CustomBinding_IRouteService">
                        <binaryMessageEncoding />
                        <httpTransport />
                    </binding>
                </customBinding>
            </bindings>
            <client>
                <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRouteService"
                    contract="BingMaps.IRouteService" name="BasicHttpBinding_IRouteService" />
                <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/binaryHttp"
                    binding="customBinding" bindingConfiguration="CustomBinding_IRouteService"
                    contract="BingMaps.IRouteService" name="CustomBinding_IRouteService" />
            </client>
        </system.serviceModel>
    </configuration>

    • created visual elements in Form1.Designer.cs with a button and a simple textbox
    • created the following Code (the Webmaster API Key signature I created via the website is cut out

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    //#####Bingmaps-Dienst einbinden
    using bingmaps;
    using bingmaps.BingMaps;
    using bingmaps.Properties;
    //#####
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace bingmaps
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
            //Webmaster-API-Schlüssel einpflegen
            // Get a key at http://www.bing.com/developers/createapp.aspx
                string key = "my_key";
            //Bing-Request-Objekt erstellen
            BingMaps.RouteRequest request = new RouteRequest();
            //Webmaster-API-Schlüssel übergeben
            request.Credentials = new Credentials { ApplicationId = key };
                //Neuen Wegpunkt in Array erstellen
                request.Waypoints = new Waypoint[]
                {
                //Brüssel
                new Waypoint(){ Location= new Location(){ Latitude = 32.45868, Longitude= -86.71458}},
                // Paris
                new Waypoint(){ Location= new Location(){ Latitude = 21.77111, Longitude= -102.35861}},
                };
                //Skala für Entfernung auf metrisches System
                request.UserProfile = new UserProfile
                    {
                    DistanceUnit = DistanceUnit.Kilometer
                    };
    
                //Routen-Optionen einstellen
                request.Options = new RouteOptions()
                {
                    //Routenmodus auf Autofahrt
                    Mode = TravelMode.Driving,
                    //Routenoptimierung auf "schnellste" Route
                    Optimization = RouteOptimization.MinimizeTime
                    //Optimierung auf "kürzeste" Route
                    //Optimization = RouteOptimization.MinimizeDistance
                };
    
            //Service-Client-Objekt erzeugen
                RouteServiceClient client = new RouteServiceClient("BasicHttpBinding_IRouteService");
            //neues Routenantwortobjekt erzeugen
            RouteResponse response = client.CalculateRoute(request);
            //neues Routenresultat-Objekt erstellen, Antwort von Calculate-Route übergeben
            RouteResult result = response.Result;
    
            //Resultat auswerten
                //wenn Resultat vorhanden
                if (result != null)
                {
                    //Antwort schreiben.
                    txtbox_distance.Text = result.Summary.Distance.ToString();
                }
                else
                {
                    //Fehlermeldung
                    txtbox_distance.Text = "Konnte Distanz nicht berechnen";
                }
            
            }
        }
    }
    

    • I compiled the application with debugging. I click on button1. After a view seconds of processing, Visual studio states the following error message:
    FautlException´1 wurde nicht behandelt:
    Credentials are either invalid or unspecified

    So what did I do wrong?


    Best regards and thanks in advance ;)

    Friday, September 06, 2013 10:40 AM

Answers