Migrate from Google Maps to TomTom
Overview
This tutorial covers some very basic use cases to help you switch your Android app from Google's APIs to TomTom's as quickly as possible. It starts with basic environment setup, then dives into the code. At the very bottom of the page is a recommended reading list to allow you to build on the foundations provided by this tutorial.
This tutorial covers:
- Getting a free API key and creating a local setup for the TomTom Maps SDK for Android.
- Initializing and displaying a map.
- Adding a marker.
- Adding a traffic layer on top of the map.
- Displaying a route.
Prerequisites
Before you start writing code, prepare your environment:
- Install Android Studio. During installation, make sure that the component "Android Virtual Device" is selected.
- If possible, start a new project (minimum SDK API 19 – Android 4.4 “KitKat”) with an Empty activity. You can use your own project as long as it meets the minimum Android SDK API level requirement
- Update the Source Compatibility and Target Compatibility to 1.8 in the Project Structure dialog ( click File > Project Structure).
- Add the TomTom repository to your project's gradle.build file:
1allprojects {2 repositories {3 google()4 jcenter()5 maven {6 url "https://repositories.tomtom.com/artifactory/maps-sdk-legacy-android"7 }8 }9}
- If you don't have an API key visit a How to get a TomTom API key site and create one.
- Create a build config fields with the API keys, which will be used later in the application:
1android {2 compileSdkVersion 293 defaultConfig {4 buildConfigField("String", "ROUTING_API_KEY", "\YOUR_KEY\")5 (...)
Initializing a map
Compare how the map is initialized using the Google and TomTom tools:
To display the map using the Google Maps SDK for Android, you need to perform a few steps:
- Add dependencies to your module's gradle.build file:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
- Add a map fragment to the main activity:
1<fragment2 android:id="@+id/mapFragment"3 android:name="com.google.android.gms.maps.SupportMapFragment"4 android:layout_width="match_parent"5 android:layout_height="match_parent"/>
- Add a Google Android API key inside the application section of the manifest.xml file:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_KEY_GOES_HERE"/>
TomTom
To do the same thing in the TomTom Maps SDK for Android:
- Add dependencies to your module's gradle.build file:
implementation 'com.tomtom.online:sdk-maps:2.4725'
- Add a map fragment to the main activity layout and API keys for Maps and Traffic services:
1(...)2xmlns:tomtom="http://schemas.android.com/apk/res-auto"3tools:context=".MainActivity">4(...)5<fragment6 android:id="@+id/mapFragment"7 android:name="com.tomtom.online.sdk.map.MapFragment"8 tomtom:mapStyleSource="STYLE_MERGER"9 android:layout_width="match_parent"10 android:layout_height="match_parent"11 tomtom:mapsApiKey="YOUR_API_KEY"12 tomtom:trafficApiKey="YOUR_API_KEY"
Displaying a marker
The next step is to allow your user to interact with the map (for instance, displaying or moving a marker).
- Implement the
OnMapReadyCallback
interface in yourMainActivity
class and override theonMapReady
method.:1@Override2 public void onMapReady(GoogleMap googleMap) {3 this.map = googleMap4 LatLng amsterdam = new LatLng(52.37, 4.90);5 googleMap.addMarker(new MarkerOptions().position(amsterdam).title("Amsterdam"));6 googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(amsterdam, 8));7} - Inside the
MainActivity
onCreate
method, get the appropriate map fragment instance and set a callback object onMainActivity
so that the onMapReady method is called:1SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()2 .findFragmentById(R.id.mapFragment);3mapFragment.getMapAsync(this);
TomTom
-
Define field
map
inMainActivity
: -
As with Google, you need to implement an
OnMapReadyCallback
interface in yourMainActivity
class and override theonMapReady
method:
Any interaction with the TomTom Map object needs to be performed after the onMapReady
callback
has been called, when the map is fully initialized.
-
Inside the
MainActivity
onCreate
method, get the map fragment instance and set a callback object onMainActivity
so that the onMapReady method is called:
Displaying traffic
Create two simple buttons to work with traffic on the map. One enables the traffic layer, and the other disables it.
In Google there is only one method responsible for traffic visualization:
googleMap.setTrafficEnabled(true);
The method displays only traffic flow tiles.
TomTom
TomTom provides two services that offer traffic information:
- Traffic flow shows the difference between current and free flow speed. Green indicates that the speeds are the same, meaning there are no traffic jams. Red indicates that traffic is much slower than free flow, meaning that there are traffic jams.
- Traffic incidents indicates specific traffic problems such as closed roads, rain, ice on the road, or accidents.
Create the buttons to handle traffic visualization and call the turnOnTrafficIncidents
and turnOnTrafficFlowTiles
methods on the map TrafficSettings object, choosing whether to display
traffic flow tiles, traffic incident tiles, both, or none.
Displaying a route/directions
The next step is to add a third button that shows a route.
Displaying a route in Google Maps SDK for Android is not straightforward. It boils down to interacting with the Directions API, gathering a list of route positions, then drawing polylines from that list directly onto the map. This is too verbose and complex to show in this tutorial.
TomTom
TomTom Routing API allows the app to calculate a route between two points, add waypoints, and draw the route on the map with one call. To enable Routing API inside your application:
-
Add a dependency to the module's gradle.build file:
implementation 'com.tomtom.online:sdk-routing:2.4725' -
Now add a button for displaying the route to the main activity layout XML file:
1<Button2android:id="@+id/btnRouteShow"3android:layout_width="wrap_content"4android:layout_height="wrap_content"5android:layout_marginBottom="8dp"6android:layout_marginEnd="8dp"7android:layout_marginStart="8dp"8android:text="@string/route_show"9app:layout_constraintBottom_toBottomOf="parent"10app:layout_constraintEnd_toEndOf="parent" /> -
Finally, add a proper handler for our new button: Planning a route requires at least two location points (e.g.: Amsterdam and Hague). Create an instance of the
RoutingApi
object by callingOnlineRoutingApi.create
method and pass a routing API key inside it. Then construct a route descriptor object where you can choose a route type (like fastest or shortest). Next, create a route calculation descriptor object which allows you to set additional routing parameters. Finally a route specification object can be created with the origin and destination points. When a route specification is prepared, it can be passed into aplanRoute
method in the routing api object. -
The
planRoute
takes aRouteCallback
where it returns a route plan object. You can getFullRoute
objects (which contains a route coordinates) from theroutePlan.getRoutes()
method, create a new RouteBuilder object from each of them, and add them to your map.
You can find full source code here: on github.
Summary
Using this tutorial, you should have converted an Android application from using Google Maps APIs to
TomTom's. Now you have a map with fresh and accurate traffic information, on which you can easily
plan many kinds of route. Great!