Plan a Trip (CPIK Libraries)
Contents
Planning a trip using the CPIK Libraries is a three-step process: clear any existing stops, create new stops using the Stop Builder, and calculate the route.
The driver can then navigate the calculated route using the CPIK navigation features.
Java
Step 1: Clear the existing trip
Start by clearing any existing stops on the route:
// Clear all stops on the route
RouteMgr.removeAllStops();
Step 2: Create stops
Use the Stop Builder to create stops on the route. In this example, we are using GPS coordinates to create the stops.
double latitudeA = 40.368420;
double longitudeA = -74.655036;
// Make new stops to add
StopBuilder sbA = StopBuilder.fromLatLon(new Coordinate(latitudeA, longitudeA));
Stop newStopToAddA = sbA.geocode(GeocodeSearchType.BEST_MATCH).get(0);
double latitudeB = 40.368530;
double longitudeB = -74.6552036;
StopBuilder sbB = StopBuilder.fromLatLon(new Coordinate(latitudeB, longitudeB));
Stop newStopToAddB = sbB.geocode(GeocodeSearchType.BEST_MATCH).get(0);
StopList stopList = new StopList();
stopList.add(newStopToAddA);
stopList.add(newStopToAddB);
Step 3: Calculate the route
Add the stops you have created, preview and calculate the route.
// Add these new stops to the trip, after the final destination
RouteMgr.addStops(RouteEnums.AddStopPurpose.AFTER_FINAL_DESTINATION, stopList, RoutePreviewMode.PREVIEW_TRIP_MAP);
// At this point, the route will not have yet been calculated, there are stops but no route
// Make the call to calculate the route
RouteMgr.calculateRoute();
// After the call, the route will be calculated between the stops on the route.
The complete example
Here’s the complete code with error handling.
try {
// Clear all stops on the route
RouteMgr.removeAllStops();
double latitudeA = 40.368420;
double longitudeA = -74.655036;
// Make new stops to add
StopBuilder sbA = StopBuilder.fromLatLon(new Coordinate(latitudeA, longitudeA));
Stop newStopToAddA = sbA.geocode(GeocodeSearchType.BEST_MATCH).get(0);
double latitudeB = 40.368530;
double longitudeB = -74.6552036;
StopBuilder sbB = StopBuilder.fromLatLon(new Coordinate(latitudeB, longitudeB));
Stop newStopToAddB = sbB.geocode(GeocodeSearchType.BEST_MATCH).get(0);
StopList stopList = new StopList();
stopList.add(newStopToAddA);
stopList.add(newStopToAddB);
// Add these new stops to the trip, after the final destination
RouteMgr.addStops(RouteEnums.AddStopPurpose.AFTER_FINAL_DESTINATION, stopList, RoutePreviewMode.PREVIEW_TRIP_MAP);
} catch (CopilotException | GeocodingException | RouteException e) {
e.printStackTrace();
}
// At this point, the route will not have yet been calculated, there are stops but no route
// Make the call to calculate the route
RouteMgr.calculateRoute();
// After the call, the route will be calculated between the stops on the route.
Objective-C
Step 1: Create stop list
Initialize a mutable array to hold your stops:
NSMutableArray *stops = [[NSMutableArray alloc] init];
Step 2: Create stops
Create stops using different StopBuilder methods:
// Create stop from postal code
StopBuilder *stopBuilder2 = [StopBuilder fromCountryAndPostalCode:@"USA" withZip:@"08540"];
Stop *stop2 = [stopBuilder2 getStop];
[stops addObject:stop2];
// Create stop from city and state
StopBuilder *stopBuilder1 = [StopBuilder fromCityAndState:@"Princeton" withState:@"NJ"];
Stop *stop1 = [stopBuilder1 getStop];
[stops addObject:stop1];
Step 3: Add stops and calculate route
Add stops to the route and calculate navigation:
// Add stops to the route after the final destination
[RouteMgr addStops:CP_AFTER_FINAL_DESTINATION withStopList:stops];
// Calculate the route
[RouteMgr calculateRoute];
The complete example
Here’s the complete iOS code:
NSMutableArray *stops = [[NSMutableArray alloc] init];
StopBuilder *stopBuilder2 = [StopBuilder fromCountryAndPostalCode:@"USA" withZip:@"08540"];
Stop *stop2 = [stopBuilder2 getStop];
[stops addObject:stop2];
StopBuilder *stopBuilder1 = [StopBuilder fromCityAndState:@"Princeton" withState:@"NJ"];
Stop *stop1 = [stopBuilder1 getStop];
[stops addObject:stop1];
[RouteMgr addStops:CP_AFTER_FINAL_DESTINATION withStopList:stops];
[RouteMgr calculateRoute];