Simple routing
Contents
The Mobile Maps SDK examples require that you first complete the initial project set-up.
Calculate a route using Trimble Maps data for commercial vehicles and draw it on the map. (Requires routing plug-in).
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
Before running the Java or Kotlin code, the theme needs to be set in the Theme.xml
file as shown below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AsyncAcctInit" parent="Theme.AppCompat" />
</resources>
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.trimblemaps.account.LicensedFeature;
import com.trimblemaps.account.TrimbleMapsAccountManager;
import com.trimblemaps.account.models.TrimbleMapsAccount;
import com.trimblemaps.api.directions.v1.TrimbleMapsDirections;
import com.trimblemaps.api.directions.v1.models.DirectionsResponse;
import com.trimblemaps.geojson.Point;
import com.trimblemaps.mapsdk.TrimbleMaps;
import com.trimblemaps.mapsdk.camera.CameraPosition;
import com.trimblemaps.mapsdk.geometry.LatLng;
import com.trimblemaps.mapsdk.maps.MapView;
import com.trimblemaps.mapsdk.maps.OnMapReadyCallback;
import com.trimblemaps.mapsdk.maps.Style;
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap;
import com.trimblemaps.mapsdk.plugins.route.Route;
import com.trimblemaps.mapsdk.plugins.route.RoutePlugin;
import com.trimblemaps.mapsdk.plugins.route.RouteRequestCallback;
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("Your-API-key-here")
.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(DirectionsResponse directionsResponse) {
Toast.makeText(MapsSDK.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);
}
/**
* Activity Overrides
*/
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.trimblemaps.account.LicensedFeature
import com.trimblemaps.account.TrimbleMapsAccountManager
import com.trimblemaps.account.models.TrimbleMapsAccount
import com.trimblemaps.api.directions.v1.TrimbleMapsDirections
import com.trimblemaps.api.directions.v1.models.DirectionsResponse
import com.trimblemaps.api.directions.v1.models.RouteOptions
import com.trimblemaps.geojson.Point
import com.trimblemaps.mapsdk.TrimbleMaps
import com.trimblemaps.mapsdk.camera.CameraPosition
import com.trimblemaps.mapsdk.geometry.LatLng
import com.trimblemaps.mapsdk.maps.MapView
import com.trimblemaps.mapsdk.maps.Style
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap
import com.trimblemaps.mapsdk.plugins.route.Route
import com.trimblemaps.mapsdk.plugins.route.RoutePlugin
import com.trimblemaps.mapsdk.plugins.route.RouteRequestCallback
class SampleRoutingActivity : Activity() {
private var mapView: MapView? = null
private var map: TrimbleMapsMap? = null
private var routePlugin: RoutePlugin? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Authorize the API key for the session.
// .apiKey() requires your Trimble Maps API key
val trimbleMapsAccount = TrimbleMapsAccount.builder()
.apiKey("Your-API-key-here")
.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 = findViewById<View>(R.id.mapView) as MapView
// the onMapReadyCallback is fired when the map is ready to be worked with
mapView!!.getMapAsync { 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 = RoutePlugin(mapView!!, map!!)
val position = CameraPosition.Builder()
.target(LatLng(40.34330490091359, -74.62327537264328))
.zoom(11.0)
.build()
map!!.cameraPosition = position
map!!.setStyle(Style.MOBILE_DAY) { createSimpleRoute() }
}
}
fun createSimpleRoute() {
// Generate the directions object using the TrimbleMapsDirections Builder
val 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)
val route: Route = Route.builder()
.id("SimpleRoute") // ID for the route
.routeOptions(directions.toRouteOptions()) // Route Options coming from TrimbleMapsDirections object
.requestCallback(object : RouteRequestCallback {
// Provide a callback to handle success and fails for the route.
override fun onRouteReady(route: DirectionsResponse?) {
Toast.makeText(this@SampleRoutingActivity, "Routing Success", Toast.LENGTH_SHORT).show()
}
override fun onRequestFailure(throwable: Throwable?, routeOptions: RouteOptions?) {
Toast.makeText(this@SampleRoutingActivity, "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)
}
/**
* Activity Overrides
*/
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
}