Creating a Route |
This section of the tutorial describes how to create a simple route from a number of given locations. The location may be given either as a LatLon (latitude/longitude) coordinate pair or as a street address.
In GeoBase a route is defined by a series of RouteStop objects. These objects would usually be created by a web form or through user input, but for this example we will hard-code the stops.
The following code fragment creates a Route between four different RouteStop objects. Note that a stop can be created from a latitude/longitude pair or a street address.
/* Create stops */ RouteStop Home = new RouteStop(new LatLon(34.1018, -118.2973)); RouteStop Cinema = new RouteStop(GeoCoder.GeoCode("1414 North Azusa Avenue, Covina, CA", Country.USA)[0].Location); RouteStop Mall = new RouteStop(GeoCoder.GeoCode("112 Plaza Drive, West Covina, CA", Country.USA)[0].Location); RouteStop Restaurant = new RouteStop(new LatLon(34.0720, -117.4962)); /* ^^ 10100 Banana Ave, Fontana, CA. */ /* Create a route */ Route myRoute = new Route(); /* Add stops to route - note that order is maintained using the AddStops() method with an array */ myRoute.AddStops(new RouteStop[] {Mall, Cinema, Home, Restaurant});
Now that the route has been created we can:
Use the OptimizeStops method to reorder the stops to minimize the total cost of the route. Note that this will override any stop order specified when the stops were added to the route.
myRoute.OptimizeStops(); /* Optimize route (optional) */
Use the GetDirections method to retrieve driving directions for the route. This method will first create an 'optimal' route between stops and then create a set of directions that may be displayed to the user or spoken aloud. This will be covered in the next section of the tutorial.
Directions myDirections = myRoute.GetDirections();
The route and direction data are rendered onto a map shown below.