Routing On Map Java¶
In this guide you will learn how to render an interactive map, compute and render a route on the map, and fly to the route.
Setup¶
First, get an API key token, see the Getting Started guide.
Download the Maps & Navigation SDK for Android archive fileDownload the RoutingOnMapJava project
archive file or clone the project with Git
See the Configure Android Example guide.
Run the example¶
In Android Studio, from the File
menu, select Sync Project with Gradle Files
An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the
android device.
This example computes and draws a route on the map
and flies to it.
Displays an interactive map which is fully 3D,
supporting pan, pinch-zoom, rotate and tilt.
How it works¶
You can open the MainActivity.java file to see how the route is computed and drawn on the map.
1routingService = new RoutingService();
A RoutingService()
is instantiated in the
MainActivityJava()
constructor, which carries out
the route computation.
1private void calculateRoute() {
2 GemCall.INSTANCE.execute(() -> {
3 ArrayList<Landmark> waypoints = new ArrayList<>();
4 waypoints.add(new Landmark("London", 51.5073204, -0.1276475));
5 waypoints.add(new Landmark("Paris", 48.8566932, 2.3514616));
6 routingService.calculateRoute(waypoints, null, false, null, null);
7 return 0;
8 });
9}
The starting, or departure point of the route is the first waypoint in a list of
2 or more Landmarks (2 in this case), each containing a name, latitude (in degrees)
and longitude (in degrees). The destination point is the last waypoint in the list.
1@Override
2protected void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.activity_main_java);
5 progressBar = findViewById(R.id.progressBar);
6 gemSurfaceView = findViewById(R.id.gem_surface);
7 SdkSettings.INSTANCE.setOnMapDataReady(isReady -> {
8 if (!isReady)
9 return null;
10 // Defines an action that should be done
11 // when the world map is ready (Updated/ loaded).
12 calculateRoute();
13 return null;
14 });
15 SdkSettings.INSTANCE.setOnApiTokenRejected(() -> {
16 Toast.makeText(this, "TOKEN REJECTED", Toast.LENGTH_LONG).show();
17 return null;
18 });
19}
The
MainActivityJava
class overrides the onCreate()
function which
calls the calculateRoute()
once the map is instantiated and ready. 1public MainActivityJava() {
2 routingService = new RoutingService();
3 routingService.setOnStarted(hasProgress -> {
4 progressBar.setVisibility(View.VISIBLE);
5 return null;
6 });
7 routingService.setOnCompleted(new Function3<ArrayList<Route>, Integer, String, Unit>() {
8 @Override
9 public Unit invoke(ArrayList<Route> routes, Integer errorCode, String hint) {
10 progressBar.setVisibility(View.GONE);
11 switch (errorCode) {
12 case GemError.NoError: {
13 GemCall.INSTANCE.execute(() -> {
14 MapView mapView = gemSurfaceView.getMapView();
15 if (mapView != null) {
16 Animation animation =
17 new Animation(EAnimation.Linear, 1000, null, null);
18 mapView.presentRoutes(routes, null, true,
19 true, true, true,
20 true, true, animation, null,
21 ERouteDisplayMode.Full, null);
22 }
23 return null;
24 });
25 break;
26 }
27 case GemError.Cancel: {
28 // The routing action was canceled
29 break;
30 }
31 default: {
32 // There was a problem in computing the route
33 Toast.makeText(MainActivityJava.this,
34 "Routing service error: ${GemError.getMessage(errorCode)}",
35 Toast.LENGTH_SHORT
36 ).show();
37 }
38 }
39 return null;
40 }
41 });
42}
The
MainActivityJava()
constructor defines the routingService.setOnStarted()
listener to show a progress bar during the route computation, and also the
routingService.setOnCompleted()
listener to stop showing the progress bar
when the route computation is completed.Then it obtains a pointer / handle to the map,
MapView mapView = gemSurfaceView.getMapView();
and starts a fly animation with a 1000msec (1 second) duration,
to center the list of possible routes in the view, that is, fly to the routes,
such that all possible route(s), if more than one was computed, between the
desired departure and destination points, fit in the viewport:Animation animation = new Animation(EAnimation.Linear, 1000, null, null);
mapView.presentRoutes(routes,...)