Migrate from Google
Overview
This tutorial covers some fundamental use cases to help you switch your iOS app from Google’s SDK to TomTom’s SDK as quickly and efficiently as possible. It starts with the basic environment setup, then goes directly into the code.
Project set up
To use Maps SDK in your application you need to obtain a TomTom API key by following these instructions. Remember to keep your API key secure.
- Install Xcode if you don’t already have it.
- Create a new project or open an existing one. The application deployment target has to be set to at least 13.0.
- Install Cocoapods on your computer.
sudo gem install cocoapods
- Install cocoapods-art tool on your computer.
sudo gem install cocoapods-art to install cocoapods-art
- Add a reference to the cocoapods-art repository:
pod repo-art add tomtom-sdk-cocoapods "https://repositories.tomtom.com/artifactory/api/pods/cocoapods"
- Create a
Podfile
in the project folder. Thepod init
command in the project folder can generate a basic podfile. - At the top of the Podfile add the source of the SDK Cocoapods.
1plugin 'cocoapods-art', :sources => [2 'tomtom-sdk-cocoapods'3]
- Add the modules that your project requires. This tutorial uses the
TomTomSDKMapDisplay
andTomTomSDKRoutePlannerOnline
modules.1TOMTOM_SDK_VERSION = '0.2.3404'23target 'YourAppTarget' do4 use_frameworks!5 pod 'TomTomSDKMapDisplay', TOMTOM_SDK_VERSION6 pod 'TomTomSDKRoutePlannerOnline', TOMTOM_SDK_VERSION7end - Install the dependencies by executing the following command in the project folder.
pod install
- To update the SDK version, run the command
pod repo-art update tomtom-sdk-cocoapods
- Open the project’s
xcworkspace
. - Create a class with the TomTom API keys. These will be used later in the application.
1private enum Keys {2 static let MAPS_KEY = "YOUR_MAPS_API_KEY"3 static let ROUTING_KEY = "YOUR_ROUTING_API_KEY"4}
Displaying a map
To display the map using the Google Maps SDK for iOS, you need to perform these steps:
- Set the Google API before performing any request.
GMSServices.provideAPIKey("GOOGLE_API_KEY")
- Initialize the
GMSMapView
with the chosen specification.mapView = GMSMapView(frame: view.frame) - Add the initialized
GMSMapView
to the parent view.view.addSubview(mapView)
TomTom SDK
To display the map using the TomTom SDK for iOS:
- Set the valid TomTom API key.
MapsDisplayService.apiKey = Keys.MAPS_KEY
- Initialize the
MapView
. It is used to display a map in the view hierarchy.let mapView = MapView(frame: view.frame) - Add the initialized
MapView
to the parent view.view.addSubview(mapView) - Most actions on the map are performed using the
Map
object. It can be accessed only when the map is fully initialized. You can learn more about it in the Adding a map guide.1mapView.getMapAsync { map in2 self.map = map3}

Displaying a marker
The next step is to allow your user to interact with the map. For instance, displaying a marker at the location where the user does a long click.
- In the Google SDK you need to set
GMSMapViewDelegate
to theGMSMapView.delegate
property to observe gesture events on the map.mapView.delegate = self - In our case, the long press action is observed and the following method is triggered.
1func mapView(_: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {2 displayMarker(at: coordinate)3}
- To display a marker at the chosen coordinates create the
GMSMarker
object. It defines the marker configuration. To add the marker to the map, assign theGMSMapView
to the marker’s property.1private func displayMarker(at coordinate: CLLocationCoordinate2D) {2 let marker = GMSMarker(position: coordinate)3 marker.map = mapView4}
TomTom SDK
- To observe gesture events performed on the map, set the
MapDelegate
to theMap
object.map.delegate = self - In this example the long click action is observed. To do it you need to override the following method of the
MapDelegate
. You can learn more by reading Events and gestures.1func map(_: TomTomMap, didLongPressOn coordinate: CLLocationCoordinate2D) {2 displayMarker(at: coordinate)3} - To display a marker at the chosen coordinates you must create
MarkerOptions
. It is used to configure the appearance of the marker. Then add theMarkerOptions
object to theMap
. If this operation fails, the exception is thrown. Learn more about working with markers in the Markers guide.1private func displayMarker(at coordinate: CLLocationCoordinate2D) {2 let markerOptions = MarkerOptions(coordinate: coordinate, pinImage: UIImage(named: "marker_pin_image")!)3 _ = try? map.addMarker(options: markerOptions)4}

Displaying traffic
Google has only one method for traffic visualization. That method can only display traffic flow tiles. To show the traffic layer:
mapView.isTrafficEnabled = true
To hide the traffic layer:
mapView.isTrafficEnabled = false
TomTom SDK
The TomTom SDK provides two kinds of traffic information, traffic flow and traffic incidents.
- 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 slower than free-flow, meaning that there are traffic jams.
map.showTraffic()map.hideTraffic()
- Traffic incidents shows specific traffic problems such as closed roads, rain, ice on the road, or accidents.
map.showTrafficIncidents()map.hideTrafficIncidents()

Displaying a route/directions
Displaying a route in the Google Maps SDK for iOS is not straightforward. It boils down to interacting with the Directions API, gathering a list of route positions, then drawing a polyline from that list directly onto the map. The instructions are not discussed in this tutorial. If you are interested in getting more detailed migration steps, Contact us.
TomTom SDK
The TomTom Routing API allows the app to easily calculate a route between two points, add waypoints, and specify other route properties. The requested Route
can then be simply drawn on the map. Detailed information about routing in the TomTom SDK can be found in the Routing module documentation. A good place to start is the Quickstart guide.
- Before using the OnlineRoutePlanner you need to provide a valid TomTom API key.
.
- Initialize
OnlineRoutePlanner
. It is the entry point for the routing service.. - Build your routing request using
RoutePlanningOptions
. You can configure the request to fit your requirements. Learn more about routing properties in the TomTom Routing API documentation.1let amsterdamCoordinate = ItineraryPoint(coordinate: CLLocationCoordinate2D(latitude: 52.3764527, longitude: 4.9062047))2let berlinCoordinate = ItineraryPoint(coordinate: CLLocationCoordinate2D(latitude: 52.5069751, longitude: 13.3631919))3let hagueCoordinate = ItineraryPoint(coordinate: CLLocationCoordinate2D(latitude: 52.078663, longitude: 4.288788))4let itinerary = Itinerary(origin: amsterdamCoordinate, destination: berlinCoordinate, waypoints: [hagueCoordinate])56let routingOptions: RoutePlanningOptions7do {8 routingOptions = try .init(9 itinerary: itinerary,10 costModel: .init(routeType: .efficient),11 vehicle: Bus()12 )13} catch {14 print("Invalid planning options: \(error.localizedDescription)")15 return16} - Perform a request using the
RoutePlanningOptions
you created previously as a parameter. A result is returned via the provided closure.1routePlanner.planRoute(options: routingOptions, onRouteReady: nil) { result in2 switch result {3 case let .success(response):4 if let route = response.routes?.first {5 self.drawRoute(route)6 }7 case .failure:8 // failure case9 break10 }11} - Finally, use the
Route
calculated by the routing service to draw it on the map. TheRouteOptions
class can be used to customize the appearance of the route. Learn more about adding a route to the map here.1private func drawRoute(_ route: TomTomSDKRoute.Route) {2 let routeOptions = RouteOptions(coordinates: route.geometry)3 _ = try? map.addRoute(routeOptions)4}
Showing user location
To access user location you must configure the following purpose strings in the Xcode build setting or in
Info.plist
:NSLocationWhenInUseUsageDescription
,NSLocationAlwaysAndWhenInUseUsageDescription
, orNSLocationAlwaysUsageDescription
. The correct key must be included or authorization requests immediately fail and the map is not be able to get the user location.
To show user location on the map in Google Maps SDK for iOS you must enable location on GMSMapView
.
mapView.isMyLocationEnabled = true
To show the location button set the following property.
mapView.settings.myLocationButton = true
TomTom SDK
To show the user location on the map, change the Map.LocationIndicator
to either userLocation
or .navigationChevron
. By default, the CLLocationManager
is used as the source of location updates. However, you can also provide your own source. Learn more about user location in the Showing the user’s location guide.
map.locationIndicatorType = .userLocation
By default the center button is hidden, so to show it you need to change its visibility using MapView
.
mapView.currentLocationButtonVisibilityPolicy = .visible
