Routing plugin
Contents
The routing plugin allows you to generate safe and legal routes for commercial vehicles and display those routes on the map. To use the plugin:
In the dependencies of the project’s gradle file, add the code below to get access to the features in the plugin.
dependencies {
implementation 'com.trimblemaps.mapsdk:maps-android-plugin-route:1.0.0'
}
When initializing the RoutePlugin
, you can customize the stop icons and the text color used to draw routes. The code below depicts how you can do this:
Setting | Description |
---|---|
mapView | The MapView to apply the route plugin to. |
trimbleMapsMap | The TrimbleMapsMap to apply route plugin with. |
belowLayer | The layer id where you’d like the route to display below. |
stopStartIconDrawable | Custom Drawable to use for the first stop (origin). |
stopEndIconDrawable | Custom Drawable to use for the last stop (destination). |
stopIconDrawable | Custom Drawable to use for the intermediate stops. |
stopIconTextColor | Custom color to use for the stop numbers on the intermediate stop icons. |
mRoutePlugin = RoutePlugin(mapView , trimbleMapsMap , belowLayer ,
stopStartIconDrawable,
stopEndIconDrawable,
bitmapDrawableFromVectorResource(R.drawable.ic_stop_blue),
Color.parseColor(“#FFFFFF”))
Once we have a valid route, we can pass that route into the RoutePlugin and it will draw the route on the map.
mRoutePlugin.addRoute(mRoute)
If you want to clear the route via the RoutePlugin, you just use clearRoutes()
mRoutePlugin.clearRoutes()
You can also frame the route passed with the frameRoutes
method. You also have the option to set custom padding to the frame if you wish to zoom out or zoom in to the route drawn.
mRoutePlugin.frameRoutes(mPaddingLeft,mPaddingTop,mPaddingRight, mPaddingBottom)
How to generate a route
In order to use the route plugin, you must first create a route to pass in. To generate a route, you need to define and initiate the RouteOptions
. If there are only two stops in a trip, then the coordinates
origin and destination should be defined and the intermediate stops can be null. If a trip has more than one stop, then you need to define the origin and destination, and put all the other stops in listOfMiddlePoints
.
Builder builder = RouteOptions.builder()
.applyDefaultParams()
.coordinates(originPoint, listOfMiddlePoints, destinationPoint)
.language(Settings.language.get(MainApplication.sharedPreferences()))
.voiceUnits(Settings.distanceUnits.get(MainApplication.sharedPreferences()))
.routeProfile(profile)
.build();
Now we can pass the routeOptions
into the requestRoutes
to get a route back from the server.
if(mTrimbleMapsNavigation != null)
mTrimbleMapsNavigation.requestRoutes(routeOptions, routeReqCallback);
In the code below we define what we want to do with the response when:
onRoutesReady
, which gives you a route when the server successfully creates one,onRoutesRequestFailure
, which gives you an error body in case anything happens, andonRoutesRequestCanceled
, which lets you define what to do in case the user decides to cancel trip.
private RoutesRequestCallback routesReqCallback = new RoutesRequestCallback() {
@Override
public onRoutesReady(List<DirectionsRoute> routes) {
// Here we will be given back a list of routes.
Timber.d(“route request success %s”, routes.toString());
if(routes.isNotEmpty()){
SharedData.directionsRoute = routes[0];
if(navigationTrimbleMapsMap != null) {
navigationTrimbleMapsMap.drawRoute(routes[0]).
animateCameraForRouteOverview(routes[0], buildRouteOverviewPaddingBeforeNavigation());
}
}
}
@Override
public onRoutesRequestFailure(Throwable throwable, RouteOptions routeOptions) {
// This will return the error
Timber.e(“route request failure %s”, throwable.toString());
}
@Override
public onRoutesRequestCanceled(RouteOptions routeOptions) {
// Allows the developer to define what to do when user cancels the route
}
}