Displaying TomTom Maps with Flutter

In the last few years, Flutter has become one of the most popular cross-platform frameworks in the world. It allows users to create an application with one code base (using a Dart language) which runs on Android or iOS, and in the future, on the Web as well.
Currently, TomTom doesn’t have a dedicated Maps SDK for Android for Flutter to display the TomTom map. In order to help developers, we’ve created a simple map application which uses an open source flutter plugin called ‘flutter_map’. The plugin implements the JavaScript Leaflet library functionalities and allows users to display a raster map from multiple providers inside the flutter screen.
Prerequesites
In order to start writing the application, a few steps need to be taken:
- First you will need a TomTom API Key. If you don't have an API key, visit the How to Get a TomTom API key tutorial and create one.
- Flutter must be installed on your system and must be added to the PATH variable.
To install Flutter, you can use instructions from the following site: https://flutter.dev/docs/get-started/install.
- The Android Studio along with the Android SDK must be installed on your system. To install Android Studio you can follow a guide here.
- Flutter and Dart plugins must be installed in the Android Studio application, just like on the following screen:
- Now you can run the flutter doctor command. When it doesn’t find any issues, then you are good to go!
Create a new Flutter project
In order to create a new Flutter application, you’ll need to create a new Flutter project and choose ‘Flutter Application’ like on the following screen:
Click ‘Next’ and give a proper name for your application, making sure that all paths are correct:
Click ‘Next’, leave all default values and click ‘Finish’:
At that point, you should be able to run a default example flutter application.
Displaying the TomTom Map
If the application is running correctly, you can start to modify a project by adding necessary dependencies into the pubspec.yaml file. Let’s add the ‘flutter_map’, http packages and run flutter pub to get:
1dependencies:2 flutter_map: 0.10.1+13 http: 0.12.2
After the new packages have been installed, let’s replace the source code in the main.dart file in order to display the TomTom map. The following code snippet is adding the ‘FlutterMap’ widget and place it in a center of the screen which is set to the TomTom office in Amsterdam.
1import "package:flutter/material.dart";2import "package:flutter_map/flutter_map.dart";3import "package:latlong/latlong.dart";4import "package:http/http.dart" as http;5import "dart:convert" as convert;67void main() => runApp(MyApp());89class MyApp extends StatelessWidget {10 @override11 Widget build(BuildContext context) {12 return MaterialApp(13 title: "Flutter Demo",14 home: HomeScreen(),15 );16 }17}1819class HomeScreen extends StatelessWidget {20 final String apiKey = "YOUR_API_KEY";2122 @override23 Widget build(BuildContext context) {24 final tomtomHQ = new LatLng(52.376372, 4.908066);25 return MaterialApp(26 title: "TomTom Map",27 home: Scaffold(28 body: Center(29 child: Stack(30 children: <Widget>[31 FlutterMap(32 options: new MapOptions(center: tomtomHQ, zoom: 13.0),33 layers: [34 new TileLayerOptions(35 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"36 "{z}/{x}/{y}.png?key={apiKey}",37 additionalOptions: {"apiKey": apiKey},38 )39 ],40 )41 ],42 )),43 ),44 );45 }46}
Adding a marker to the map
In order to add a marker to the map, a developer needs to add an additional marker layer inside the FlutterMap widget, so that it looks like this:
1FlutterMap(2 options: new MapOptions(center: tomtomHQ, zoom: 13.0),3 layers: [4 new TileLayerOptions(5 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"6 "{z}/{x}/{y}.png?key={apiKey}",7 additionalOptions: {"apiKey": apiKey},8 ),9 new MarkerLayerOptions(10 markers: [11 new Marker(12 width: 80.0,13 height: 80.0,14 point: tomtomHQ,15 builder: (BuildContext context) => const Icon(16 Icons.location_on,17 size: 60.0,18 color: Colors.black),19 ),20 ],21 ),22 ],23)
Adding the TomTom logo image
According to the Terms and Conditions of TomTom Maps API, a developer also needs to add a TomTom logo to the application. Let’s download the image from and place it in the newly created images folder:
In order to use a newly created logo in the application, a new asset needs to be added inside the pubspec.yaml inside the ‘flutter’ section:
1assets:23 - images/tt_logo.png
Now the image can be added as a new child of the Stack widget, right next to the FlutterMap widget. The image is wrapped inside a Container so that it can be positioned easily on the screen:
1@override2Widget build(BuildContext context) {3 final tomtomHQ = new LatLng(52.376372, 4.908066);4 return MaterialApp(5 title: "TomTom Map",6 home: Scaffold(7 body: Center(8 child: Stack(9 children: <Widget>[10 FlutterMap(11 options: new MapOptions(center: tomtomHQ, zoom: 13.0),12 layers: [13 new TileLayerOptions(14 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"15 "{z}/{x}/{y}.png?key={apiKey}",16 additionalOptions: {"apiKey": apiKey},17 ),18 new MarkerLayerOptions(19 markers: [20 new Marker(21 width: 80.0,22 height: 80.0,23 point: new LatLng(52.376372, 4.908066),24 builder: (BuildContext context) => const Icon(25 Icons.location_on,26 size: 60.0,27 color: Colors.black),28 ),29 ],30 ),31 ],32 ),33 Container(34 padding: EdgeInsets.all(20),35 alignment: Alignment.bottomLeft,36 child: Image.asset("images/tt_logo.png"))37 ],38 )),39 ),40 );41}
After saving the file, the TomTom logo should appear at the bottom left side of the screen just like on the following screen:
Implementing the TomTom Copyright API
According to the Terms and Conditions, a developer needs to implement the Copyright API as well. Let’s do that by adding a simple floating action button to the application Scaffold widget in the Home section:
1@override2Widget build(BuildContext context) {3 final tomtomHQ = new LatLng(52.376372, 4.908066);4 return MaterialApp(5 title: "TomTom Map",6 home: Scaffold(7 body: Center(8 child: Stack(9 children: <Widget>[10 FlutterMap(11 options: new MapOptions(center: tomtomHQ, zoom: 13.0),12 layers: [13 new TileLayerOptions(14 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"15 "{z}/{x}/{y}.png?key={apiKey}",16 additionalOptions: {"apiKey": apiKey},17 ),18 new MarkerLayerOptions(19 markers: [20 new Marker(21 width: 80.0,22 height: 80.0,23 point: new LatLng(52.376372, 4.908066),24 builder: (BuildContext context) => const Icon(25 Icons.location_on,26 size: 60.0,27 color: Colors.black),28 ),29 ],30 ),31 ],32 ),33 Container(34 padding: EdgeInsets.all(20),35 alignment: Alignment.bottomLeft,36 child: Image.asset("images/tt_logo.png"))37 ],38 )),39 floatingActionButton: FloatingActionButton(40 child: Icon(Icons.copyright),41 onPressed: () async {42 },43 ),44 ),45 );46}
A new Floating Action Button should appear on the application screen, just like on the following image:
Now let’s add a new file which will contain a simple widget displaying a scrollable text.
Put the following source code in the newly added copyrights_page.dart file:
1import 'package:flutter/material.dart';23class CopyrightsPage extends StatelessWidget {4 final String copyrightsText;56 CopyrightsPage({Key key, @required this.copyrightsText}) : super(key: key);78 @override9 Widget build(BuildContext context) {10 return Scaffold(11 appBar: AppBar(12 title: Text("TomTom Maps API - Copyrights"),13 ),14 body: Container(15 child: Column(16 children: [17 Expanded(18 child: SingleChildScrollView(19 child: Container(20 padding: EdgeInsets.all(20), child: Text(copyrightsText)),21 )),22 ],23 ),24 ),25 );26 }27}
And import the new copyrights_page.dart file inside a main.dart:
import "package:flutter_app/copyrights_page.dart";
Now let’s use the TomTom Copyrights API by creating the getCopyrightsJSONResponse() method and call it when the Floating Action Button will be pressed.
1 @override2 Widget build(BuildContext context) {3 final tomtomHQ = new LatLng(52.376372, 4.908066);4 return MaterialApp(5 title: "TomTom Map",6 home: Scaffold(7 body: Center(8 child: Stack(9 children: <Widget>[10 FlutterMap(11 options: new MapOptions(center: tomtomHQ, zoom: 13.0),12 layers: [13 new TileLayerOptions(14 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"15 "{z}/{x}/{y}.png?key={apiKey}",16 additionalOptions: {"apiKey": apiKey},17 ),18 new MarkerLayerOptions(19 markers: [20 new Marker(21 width: 80.0,22 height: 80.0,23 point: new LatLng(52.376372, 4.908066),24 builder: (BuildContext context) => const Icon(25 Icons.location_on,26 size: 60.0,27 color: Colors.black),28 ),29 ],30 ),31 ],32 ),33 Container(34 padding: EdgeInsets.all(20),35 alignment: Alignment.bottomLeft,36 child: Image.asset("images/tt_logo.png"))37 ],38 )),39 floatingActionButton: FloatingActionButton(40 child: Icon(Icons.copyright),41 onPressed: () async {42 http.Response response = await getCopyrightsJSONResponse();43 },44 ),45 ),46 );47 }4849 Future<http.Response> getCopyrightsJSONResponse() async {50 var url = "https://api.tomtom.com/map/1/copyrights.json?key=$apiKey";51 var response = await http.get(url);52 return response;53 }5455}
In order to parse the response from the API, let’s create the parseCopyrightsResponse method along with a few more helper methods like parseRegionsCopyrights and parseGeneralCopyrights. Pass the parsing results into the copyrights screen and show it by calling using the Navigator:
1import "package:flutter/material.dart";2import "package:flutter_map/flutter_map.dart";3import "package:latlong/latlong.dart";4import "package:http/http.dart" as http;5import "dart:convert" as convert;6import "package:flutter_app/copyrights_page.dart";78void main() => runApp(MyApp());910class MyApp extends StatelessWidget {11 @override12 Widget build(BuildContext context) {13 return MaterialApp(14 title: "Flutter Demo",15 home: HomeScreen(),16 );17 }18}1920class HomeScreen extends StatelessWidget {21 final String apiKey = "YOUR_API_KEY";2223 @override24 Widget build(BuildContext context) {25 final tomtomHQ = new LatLng(52.376372, 4.908066);26 return MaterialApp(27 title: "TomTom Map",28 home: Scaffold(29 body: Center(30 child: Stack(31 children: <Widget>[32 FlutterMap(33 options: new MapOptions(center: tomtomHQ, zoom: 13.0),34 layers: [35 new TileLayerOptions(36 urlTemplate: "https://api.tomtom.com/map/1/tile/basic/main/"37 "{z}/{x}/{y}.png?key={apiKey}",38 additionalOptions: {"apiKey": apiKey},39 ),40 new MarkerLayerOptions(41 markers: [42 new Marker(43 width: 80.0,44 height: 80.0,45 point: new LatLng(52.376372, 4.908066),46 builder: (BuildContext context) => const Icon(47 Icons.location_on,48 size: 60.0,49 color: Colors.black),50 ),51 ],52 ),53 ],54 ),55 Container(56 padding: EdgeInsets.all(20),57 alignment: Alignment.bottomLeft,58 child: Image.asset("images/tt_logo.png"))59 ],60 )),61 floatingActionButton: FloatingActionButton(62 child: Icon(Icons.copyright),63 onPressed: () async {64 http.Response response = await getCopyrightsJSONResponse();6566 Navigator.push(67 context,68 MaterialPageRoute(69 builder: (context) => CopyrightsPage(70 copyrightsText: parseCopyrightsResponse(response))));71 },72 ),73 ),74 );75 }7677 Future<http.Response> getCopyrightsJSONResponse() async {78 var url = "https://api.tomtom.com/map/1/copyrights.json?key=$apiKey";79 var response = await http.get(url);80 return response;81 }8283 String parseCopyrightsResponse(http.Response response) {84 if (response.statusCode == 200) {85 StringBuffer stringBuffer = StringBuffer();86 var jsonResponse = convert.jsonDecode(response.body);87 parseGeneralCopyrights(jsonResponse, stringBuffer);88 parseRegionsCopyrights(jsonResponse, stringBuffer);89 return stringBuffer.toString();90 }91 return "Can't get copyrights";92 }9394 void parseRegionsCopyrights(jsonResponse, StringBuffer sb) {95 List<dynamic> copyrightsRegions = jsonResponse["regions"];96 copyrightsRegions.forEach((element) {97 sb.writeln(element["country"]["label"]);98 List<dynamic> cpy = element["copyrights"];99 cpy.forEach((e) {100 sb.writeln(e);101 });102 sb.writeln("");103 });104 }105106 void parseGeneralCopyrights(jsonResponse, StringBuffer sb) {107 List<dynamic> generalCopyrights = jsonResponse["generalCopyrights"];108 generalCopyrights.forEach((element) {109 sb.writeln(element);110 sb.writeln("");111 });112 sb.writeln("");113 }114}
Now the copyrights screen should be visible:
The full source code of the application can be found here in our GitHub.
Happy coding!