Bing is cool! (at least the map part)
I loaded up Visual Studio and fixed a developer key at www.bingmapsportal.com and then I spent an hour hacking with the Bing web services for routes.
I wanted to create an easy function to get directions from coordinate A to coordinate B and I wanted it to be asynchronous. Oh, yeah, I didn't care much about performance or any other details, just wanted results. After all a hack is a hack.
So. This is how I did it. Step by step.
1. Get a key to bing maps.
2. Create a service reference to http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc?wsdl
3. Created a route class with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapTools.BingRouteReference;
namespace MapTools
{
public class Routes
{
public delegate void RouteCompletedHandler(object sender, RouteFinishedEventArgs e);
public event RouteCompletedHandler RouteCompleted;
public void Calculate(Location start, Location end)
{
BingRouteReference.RouteServiceClient routeService = new
BingRouteReference.RouteServiceClient();
RouteRequest routeRequest = new RouteRequest();
routeRequest.Credentials = new Credentials();
routeRequest.Credentials.ApplicationId = "[put your map key here!]";
routeRequest.Waypoints = new Waypoint[2];
routeRequest.Waypoints[0] = new Waypoint();
routeRequest.Waypoints[1] = new Waypoint();
routeRequest.Waypoints[0].Location = start;
routeRequest.Waypoints[1].Location = end;
routeService.CalculateRouteCompleted += new
EventHandler<CalculateRouteCompletedEventArgs>
(routeService_CalculateRouteCompleted);
routeService.CalculateRouteAsync(routeRequest);
}
void routeService_CalculateRouteCompleted(object sender,
CalculateRouteCompletedEventArgs e)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (var item in e.Result.Result.Legs.SelectMany(s=>s.Itinerary) )
{
stringBuilder.AppendLine(item.Text);
}
RouteFinishedEventArgs eventArgs = new RouteFinishedEventArgs()
{RouteDescription = stringBuilder.ToString()};
RouteCompleted(this, eventArgs);
}
}
public class RouteFinishedEventArgs
{
public string RouteDescription { get; set; }
}
}
4. Created a client (winform is still quicker for trying out hacks):
Routes routes = new Routes();
public FormTester()
{
InitializeComponent();
routes.RouteCompleted += new
Routes.RouteCompletedHandler(routes_RouteCompleted);
}
private void buttonExecute_Click(object sender, EventArgs e)
{
//Stockholm to Oslo
routes.Calculate(
new MapTools.BingRouteReference.Location()
{ Latitude = 59.32944, Longitude = 18.06861 },
new MapTools.BingRouteReference.Location()
{ Latitude = 59.94944, Longitude = 10.75639 });
}
void routes_RouteCompleted(object sender, RouteFinishedEventArgs e)
{
textBoxResults.Text = e.RouteDescription;
}
5. Sat back and enjoyed the directions:
<VirtualEarth:Action>Depart</VirtualEarth:Action> <VirtualEarth:RoadName>Gustav Adolfs Torg</VirtualEarth:RoadName> toward <VirtualEarth:Toward>Malmtorgsgatan</VirtualEarth:Toward>
<VirtualEarth:Action>Bear</VirtualEarth:Action> <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Norrbro</VirtualEarth:RoadName>
<VirtualEarth:Action>Turn</VirtualEarth:Action> <VirtualEarth:TurnDir>left</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Slottskajen</VirtualEarth:RoadName>
<VirtualEarth:Action>Bear</VirtualEarth:Action> <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Skeppsbron</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>Södermalmstorg</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>Västra Slussgatan</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>Hornsgatan</VirtualEarth:RoadName>
<VirtualEarth:Action>Bear</VirtualEarth:Action> <VirtualEarth:TurnDir>left</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Långholmsgatan</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>Liljeholmsbron</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>Södertäljevägen</VirtualEarth:RoadName>
At exit <VirtualEarth:ExitNumber>Södertäljevägen</VirtualEarth:ExitNumber>, <VirtualEarth:Action>take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>left</VirtualEarth:TurnDir> for <VirtualEarth:RoadName>E20 / E4</VirtualEarth:RoadName> toward <VirtualEarth:Sign>Helsingborg / Göteborg</VirtualEarth:Sign>
At exit <VirtualEarth:ExitNumber>Trafikplats Saltskog östra</VirtualEarth:ExitNumber>, <VirtualEarth:Action>take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> for <VirtualEarth:RoadName>E20</VirtualEarth:RoadName> toward <VirtualEarth:Sign>Strängnäs / Södertälje C / Göteborg</VirtualEarth:Sign>
<VirtualEarth:Action>At roundabout</VirtualEarth:Action>, <VirtualEarth:Action>take</VirtualEarth:Action> <VirtualEarth:ExitNumber>1st</VirtualEarth:ExitNumber> exit
At exit <VirtualEarth:ExitNumber>Trafikplats Gräsnäs</VirtualEarth:ExitNumber>, <VirtualEarth:Action>take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> for <VirtualEarth:RoadName>E18 / 249 / E20</VirtualEarth:RoadName> toward <VirtualEarth:Sign>Örebro / Göteborg / Lindesberg / Oslo</VirtualEarth:Sign>
At exit <VirtualEarth:ExitNumber>110A</VirtualEarth:ExitNumber>, <VirtualEarth:Action>take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> for <VirtualEarth:RoadName>E18</VirtualEarth:RoadName> toward <VirtualEarth:Sign>Örebro flygplats / Karlskoga / Oslo</VirtualEarth:Sign>
<VirtualEarth:Action>Pass through 2 roundabouts</VirtualEarth:Action>, remaining on <VirtualEarth:RoadName>Örebrovägen</VirtualEarth:RoadName>
<VirtualEarth:Action>At roundabout</VirtualEarth:Action>, <VirtualEarth:Action>take</VirtualEarth:Action> <VirtualEarth:ExitNumber>2nd</VirtualEarth:ExitNumber> exit onto <VirtualEarth:RoadName>E18 / E45</VirtualEarth:RoadName>
<VirtualEarth:Action>Keep</VirtualEarth:Action> <VirtualEarth:TurnDir>straight</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>E18 / Orrhagen</VirtualEarth:RoadName>
<VirtualEarth:Action>Pass through 4 roundabouts</VirtualEarth:Action>, remaining on <VirtualEarth:RoadName>Trøgstadveien</VirtualEarth:RoadName>
At exit <VirtualEarth:ExitNumber>Vinterbrokrysset</VirtualEarth:ExitNumber>, <VirtualEarth:Action>take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>left</VirtualEarth:TurnDir> for <VirtualEarth:RoadName>E6</VirtualEarth:RoadName> toward <VirtualEarth:Sign>Oslo</VirtualEarth:Sign>
<VirtualEarth:Action>Keep</VirtualEarth:Action> <VirtualEarth:TurnDir>straight</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>E6</VirtualEarth:RoadName>
<VirtualEarth:Action>Road name changes</VirtualEarth:Action> to <VirtualEarth:RoadName>150 / Hjalmar Brantings Vei / Ring 3</VirtualEarth:RoadName>
<VirtualEarth:Action>Take</VirtualEarth:Action> ramp <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> toward <VirtualEarth:Sign>Tåsen / Maridalen</VirtualEarth:Sign>
<VirtualEarth:Action>At roundabout</VirtualEarth:Action>, <VirtualEarth:Action>take</VirtualEarth:Action> <VirtualEarth:ExitNumber>2nd</VirtualEarth:ExitNumber> exit onto <VirtualEarth:RoadName>Rolf Wickstrøms Vei</VirtualEarth:RoadName>
<VirtualEarth:Action>At roundabout</VirtualEarth:Action>, <VirtualEarth:Action>take</VirtualEarth:Action> <VirtualEarth:ExitNumber>1st</VirtualEarth:ExitNumber> exit onto <VirtualEarth:RoadName>Maridalsveien</VirtualEarth:RoadName>
<VirtualEarth:Action>Turn</VirtualEarth:Action> <VirtualEarth:TurnDir>right</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Blåsbortveien</VirtualEarth:RoadName>
<VirtualEarth:Action>Turn</VirtualEarth:Action> <VirtualEarth:TurnDir>left</VirtualEarth:TurnDir> onto <VirtualEarth:RoadName>Ustvedts Vei</VirtualEarth:RoadName>
<VirtualEarth:Action>Arrive</VirtualEarth:Action> at <VirtualEarth:WaypointName>Ustvedts Vei</VirtualEarth:WaypointName>
So now you know how to get to Norway...
PS. Just let me know if you want the full code.
No comments:
Post a Comment