Migrate from Google

VERSION 0.2.2746
PUBLIC PREVIEW

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.

  1. Install Xcode if you don’t already have it.
  2. Create a new project or open an existing one. The application deployment target has to be set to at least 13.0.
  3. Install Cocoapods on your computer.
    sudo gem install cocoapods
  4. Install cocoapods-art tool on your computer.
    sudo gem install cocoapods-art to install cocoapods-art
  5. Add a reference to the cocoapods-art repository:
    pod repo-art add tomtom-sdk-cocoapods "https://repositories.tomtom.com/artifactory/api/pods/cocoapods"
  6. Create a Podfile in the project folder. The pod init command in the project folder can generate a basic podfile.
  7. At the top of the Podfile add the source of the SDK Cocoapods.
    1plugin 'cocoapods-art', :sources => [
    2 'tomtom-sdk-cocoapods'
    3]
  8. Add the modules that your project requires. This tutorial uses the TomTomSDKMapDisplay and TomTomSDKRoutePlannerOnline modules.
    1TOMTOM_SDK_VERSION = '0.2.2746'
    2
    3target 'YourAppTarget' do
    4 use_frameworks!
    5 pod 'TomTomSDKMapDisplay', TOMTOM_SDK_VERSION
    6 pod 'TomTomSDKRoutePlannerOnline', TOMTOM_SDK_VERSION
    7end
  9. Install the dependencies by executing the following command in the project folder.
    pod install
  10. To update the SDK version, run the command
    pod repo-art update tomtom-sdk-cocoapods
  11. Open the project’s xcworkspace.
  12. 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

Google

To display the map using the Google Maps SDK for iOS, you need to perform these steps:

  1. Set the Google API before performing any request.
    GMSServices.provideAPIKey("GOOGLE_API_KEY")
  2. Initialize the GMSMapView with the chosen specification.
    mapView = GMSMapView(frame: view.frame)
  3. Add the initialized GMSMapView to the parent view.
    view.addSubview(mapView)

TomTom SDK

To display the map using the TomTom SDK for iOS:

  1. Set the valid TomTom API key.
    MapsDisplayService.mapsKey = Keys.MAPS_KEY
  2. Initialize the MapView. It is used to display a map in the view hierarchy.
    let mapView = MapView(frame: view.frame)
  3. Add the initialized MapView to the parent view.
    view.addSubview(mapView)
  4. 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 in
    2 self.map = map
    3}

center

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.

Google

  1. In the Google SDK you need to set GMSMapViewDelegate to the GMSMapView.delegate property to observe gesture events on the map.
    mapView.delegate = self
  2. 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}
  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 the GMSMapView to the marker’s property.
    1private func displayMarker(at coordinate: CLLocationCoordinate2D) {
    2 let marker = GMSMarker(position: coordinate)
    3 marker.map = mapView
    4}

TomTom SDK

  1. To observe gesture events performed on the map, set the MapDelegate to the Map object.
    map.delegate = self
  2. 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(_: Map, didLongPressOn coordinate: CLLocationCoordinate2D) {
    2 displayMarker(at: coordinate)
    3}
  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 the MarkerOptions object to the Map. 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}

center

Displaying traffic

Google

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()

center

Displaying a route/directions

Google

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.

  1. Before using the OnlineRoutePlanner you need to provide a valid TomTom API key.
    OnlineRoutingService.routingKey = Keys.ROUTING_KEY
  2. Initialize OnlineRoutePlanner. It is the entry point for the routing service.
    .
  3. 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])
    5
    6let routingOptions: RoutePlanningOptions
    7do {
    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 return
    16}
  4. 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 in
    2 switch result {
    3 case let .success(response):
    4 if let route = response.routes?.first {
    5 self.drawRoute(route)
    6 }
    7 case .failure:
    8 // failure case
    9 break
    10 }
    11}
  5. Finally, use the Route calculated by the routing service to draw it on the map. The RouteOptions 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, or NSLocationAlwaysUsageDescription. The correct key must be included or authorization requests immediately fail and the map is not be able to get the user location.

Google

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

center