Skip to main content

Simple routing

Contents

Calculate a route using Trimble Maps data for commercial vehicles and draw it on the map. (Requires routing plug-in ).

lines-on-a-map

activity_sample_routing.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">
    <com.trimblemaps.mapsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

SampleRoutingActivity.java

public class SampleRoutingActivity extends AppCompatActivity {
    private MapView mapView;
    private TrimbleMapsMap map;
    private RoutePlugin routePlugin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Authorize the api key for the session.
        // .apiKey() requires your Trimble Maps API key
        TrimbleMapsAccount trimbleMapsAccount = TrimbleMapsAccount.builder()
                .apiKey(getString(R.string.API_KEY))
                .addLicensedFeature(LicensedFeature.MAPS_SDK)
                .build();

        // Initialize the session
        TrimbleMapsAccountManager.initialize(trimbleMapsAccount);
        TrimbleMapsAccountManager.awaitInitialization();

        // Get an instance of the map, done before the layout is set.
        TrimbleMaps.getInstance(this);
        setContentView(R.layout.activity_sample_routing);

        // Set up the MapView from the layout
        mapView = (MapView) findViewById(R.id.mapView);

        // the onMapReadyCallback is fired when the map is ready to be worked with
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull TrimbleMapsMap trimbleMapsMap) {
                // The TrimbleMapsMap object is created, now a style can be applied to render a map.

                map = trimbleMapsMap;

                // Create the Route Plugin, giving it the map view it's to draw to
                routePlugin = new RoutePlugin(mapView, map);
                CameraPosition position = new CameraPosition.Builder()
                        .target(new LatLng(40.34330490091359, -74.62327537264328))
                        .zoom(11)
                        .build();
                map.setCameraPosition(position);
                map.setStyle(Style.MOBILE_DAY, new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {
                        createSimpleRoute();

                    }
                });
            }
        });

    }

    public void createSimpleRoute() {
        // Generate the directions object using the TrimbleMapsDirections Builder
        TrimbleMapsDirections directions = TrimbleMapsDirections.builder()
                .origin(Point.fromLngLat(-74.59977385874882, 40.361202627269634))
                .destination(Point.fromLngLat(-74.77334837439244,40.23296852563686))
                .build();

        // Create a Route object, giving it an id (used for framing later)
        Route route = Route.builder()
                .id("SimpleRoute") // ID for the route
                .routeOptions(directions.toRouteOptions()) // Route Options coming from TrimbleMapsDirections object
                .requestCallback(new RouteRequestCallback() { // Provide a callback to handle success and fails for the route.
                    @Override
                    public void onRouteReady(com.trimblemaps.api.directions.v1.models.DirectionsRoute route) {
                        Toast.makeText(SampleRoutingActivity.this, "Routing Success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRequestFailure(Throwable throwable, com.trimblemaps.api.directions.v1.models.RouteOptions routeOptions) {
                        Toast.makeText(SampleRoutingActivity.this, "Routing Fail", Toast.LENGTH_SHORT).show();
                    }
                })
                .color(Color.MAGENTA) // Give the line path a colour
                .build();

        // use the route plugin to add the route to the map and frame it.
        routePlugin.addRoute(route);
        routePlugin.frameRoute("SimpleRoute", 30);
    }
}
Last updated September 5, 2022.
Contents