The RouteService provided by Bing Maps allows you to compute routes and routes on Maps in multiple ways, such as driving routes and locations, and travel routes. You can add a Web service reference to the service through the Service's address (http://dev.virtualearth.net/WebServices/v1/RouteService/RouteService.svc.
The routing function service provides two methods: CalculateRoute () and CalculateRoutesFormMajorRoads (). The functions are as follows:
1. CalculateRoute: calculates the path, returns the specified path itinerary, and other related path data (including the road name and travel method) of the path ).
2. CalculateRoutesFormMajorRoads: calculates the specified geographical location point or path direction and nearby road information.
To give a clearer introduction to the Interface Usage of the routing service, we will introduce the detailed application of the routing service interface through an instance. FromChengdu to ChongqingThen Mark the route map from Chengdu to Chongqing and the itinerary of the longitude city in the middle.
After a service reference is added to the RouteService, The Silverlight client automatically generates the client proxy object of the service, encapsulate RouteServiceClient and specify the event processing function client_CalculateRouteCompleted after the interface is called. The following code block:
Private RouteServiceClient client;
Public RouteServiceClient Client
{
Get
{
If (client = null)
{
Bool uriScheme =! Application. Current. IsRunningOutOfBrowser & HtmlPage. Document. DocumentUri. Scheme. Equals (Uri. UriSchemeHttps );
BasicHttpBinding binding = new BasicHttpBinding (uriScheme? BasicHttpSecurityMode. Transport: BasicHttpSecurityMode. None );
Binding. maxcompute edmessagesize = int. MaxValue;
Binding. MaxBufferSize = int. MaxValue;
UriBuilder uri = new UriBuilder ("http://dev.virtualearth.net/webservices/v1/RouteService/RouteService.svc ");
If (uriScheme)
{
Uri. Scheme = Uri. UriSchemeHttps;
Uri. Port =-1;
}
Client = new RouteServiceClient (binding, new EndpointAddress (uri. Uri ));
// Call the event processing function after completion
Client. CalculateRouteCompleted + = new EventHandler <CalculateRouteCompletedEventArgs> (client_CalculateRouteCompleted );
}
Return client;
}
}
The call of the route Retrieval Interface is very simple. to retrieve the path travel between those locations, you only need to map the geographic location coordinates (longitude and latitude) of the location) you can pass it to the route request object. Detailed code blocks are as follows:
Private void btnCalculateRoute_Click (object sender, RoutedEventArgs e)
{
Var request = new RouteRequest ();
Request. Credentials = new Credentials ();
Request. Credentials. ApplicationId = "credential ";
// Set the start and end coordinate points
Var waypoints = new System. Collections. ObjectModel. ObservableCollection <Waypoint>
{
New Waypoint () {Description = "Chengdu", Location = new Location () {Latitude = 30.6666587469201, longpolling = 104.062021177233 }},
New Waypoint () {Description = "Chongqing", Location = new Location () {Latitude = 29.5076372217973, longpolling = 106.489384971208 }},
};
Request. Waypoints = waypoints;
Request. ExecutionOptions = new ExecutionOptions ();
Request. ExecutionOptions. SuppressFaults = true;
Request. Options = new RouteOptions ();
Request. Options. RoutePathType = RoutePathType. Points;
Client. CalculateRouteAsync (request );
}
As defined in the Code block above, the route point (Waypoint) of the route request object (RouteRequest) is constructed by constructing the two geographical coordinates of Chengdu and Chongqing ), then, call the routing Service Interface Method to retrieve the path travel line. The return value of the final interface is handed over to the result Processing Event function specified during the encapsulation process in the RouteServiceClient above for processing. here we need to analyze the specific points.
1. In the event processing function for Interface request Completion, you can obtain the RouteResponse, including a series of result parameters such as status, rules, and path points.
2. You can use rules to detect or obtain the number of path points between two or more distance points.
3. If you know the path point (through the geographic coordinates of the point), you can draw a line that does not estimate the value on the map to represent the path line.
4. You can mark a path as a path point through a special symbol, such as a circle, triangle, or an image with special meanings.
5. Summarize the above four points and draw a conclusion. After you call the path Retrieval Interface to obtain the path coordinate, draw a path map on the map and mark the path with a circle.
Through the above analysis, we have already clearly described the following things in Chinese. The following code block is the code that implements the above five points:
Private void client_CalculateRouteCompleted (object sender, CalculateRouteCompletedEventArgs e)
{
RouteResponse routeResponse = e. Result;
Try
{
If (e. Result. ResponseSummary. StatusCode! = ResponseStatusCode. Success)
{
MessageBox. Show ("wrong route status ");
}
Else if (e. Result. Result. Legs. Count = 0)
{
MessageBox. Show ("no routing rule found ");
}
Else
{
// Initialize the coordinates of Route points and other attributes
MapPolyline line = new MapPolyline ();
Line. Locations = new LocationCollection ();
Line. Stroke = new SolidColorBrush (Colors. Blue );
Line. Opacity = 0.66;
Line. StrokeThickness = 5;
Foreach (var point in e. Result. Result. RoutePath. Points)
{
Line. Locations. Add (new Location (point. Latitude, point. longpolling ));
}
RouteLayer. Children. Add (line );
// Record start point and end point
LocationRect rect = new LocationRect (line. Locations [0], line. Locations [line. Locations. Count-1]);
// Traverse the route points in the processing service call interface, and draw a red circle mark for each travel point
Foreach (var item in e. Result. Result. Legs [0]. Itinerary)
{
Ellipse ellipse = new Ellipse () {Width = 10, Height = 10, Fill = new SolidColorBrush (Colors. Red), Opacity = 0.66, Tag = item };
Location location = new Location (item. Location. Latitude, item. Location. longpolling );
MapLayer. SetPosition (ellipse, location );
MapLayer. SetPositionOrigin (ellipse, PositionOrigin. Center );
RouteLayer. Children. Add (ellipse );
}
Map. SetView (rect );
}
}
Catch (Exception)
{
MessageBox. Show ("exceptions occurred during route resolution ");
}
}
The RouteLayer used in the program code adds a MapLaye layer to the map Control. r is used to present the path itinerary and path tag and directly uses the MapLayer provided by Bing Maps Silverlight Control, the following code block:
<M: Map CredentialsProvider = "Bing Maps Development Key" x: Name = "map">
<M: MapLayer x: Name = "RouteLayer"> </m: MapLayer>
</M: Map>
Shows the final effect of compiling and running:
For more information about the Bing Maps map service, see:
MSDN: http://msdn.microsoft.com/en-us/library/cc980922.aspx
Bing Maps development site: http://www.microsoft.com/maps/developers/
Bing Maps SDK: http://msdn.microsoft.com/en-us/library/dd877180.aspx
Copyright description
This article is an original article. You are welcome to repost it and indicate the source of the Article. Its copyright belongs to the author and the blog Park.
Author: Beniao, Microsoft Bing Maps Development Group: 75662563
Article Source: http://beniao.cnblogs.com/or http://www.cnblogs.com/