Personal Data
To use the Maps SDK in your application you need to obtain a TomTom API key by following these instructions. Remember to keep your API key secure.
Introduction
The Personal Data modules provide easy and efficient access to a user’s personal data. This data encompasses home, work, favorite locations, and recent destinations. Based on their relevance, the user can designate these locations to search, routing, and navigation. These features are important for creating navigation applications that enhance the user experience.
By the end of this guide, you will be able to use the Personal Data modules to manage a user’s profile in your Android application, allowing for a more personalized and seamless navigation experience.
Project setup
The Personal Data functionality is divided into two modules:
- The
personal-data-api
interface module - The
personal-data
implementation module.
Configure the project as described in the Project setup guide.
Then add the personal-data-api
and personal-data
module dependencies in the build.gradle.kts
file of your application module and synchronize the project.
implementation("com.tomtom.sdk.personaldata:personal-data-api:1.20.1")implementation("com.tomtom.sdk.personaldata:personal-data:1.20.1")
User profiles
To manage user profiles in your application, you must work with the PersonalData interface and its implementation. This provides you with methods for loading, storing, and clearing the user profile. It also allows you to get update notifications on changes to the user profile data.
The user data is represented in memory as a UserProfile object, containing the user’s personal locations, such as home, work, favorites, and recent destinations.
Note: Whenever you change the user profile data, you must store the user profile in order to persist the changes, otherwise they are saved only in memory and lost when the application is closed.
Working with offline personal data
First, create an instance of PersonalData using the personal data factory. You’ll need to specify a directory for storing the data.
Further, this is how can use the PersonalData instance to manage the user profile.
1val personalData: PersonalData = PersonalDataFactory.create(2 context = context,3 storeageDir = File("path/to/personal/data")4)56// Loading the user profile in memory.7var userProfile: UserProfile = personalData.loadUserProfile().fold(8 ifSuccess = { it },9 ifFailure = { failure ->10 // There was an error loading the user profile; use an empty user profile instead.11 UserProfile()12 }13)1415// Modifying the in-memory user profile.16userProfile.locations.create(17 name = "Royal Home",18 types = setOf(PersonalLocationType.Favorite),19 place = Place(20 coordinate = GeoPoint(21 latitude = 52.373207,22 longitude = 4.89139123 ),24 address = Address(25 freeformAddress = "Royal Palace, Amsterdam, Netherlands"26 )27 )28)2930// Storing the user profile.31userProfile = personalData.storeUserProfile(userProfile).fold(32 ifSuccess = { it },33 ifFailure = { failure ->34 // There was an error storing the user profile, use the previous user profile.35 userProfile36 }37)
Personal locations
Personal locations refer to the user’s home, work, favorites, and recent destinations.
A personal location is represented by the PersonalLocation class, which contains the following useful properties:
- id: A unique identifier for the location.
- name: The personal name of the location.
- place: The place of the location, which includes information such as coordinates, address and the personal location types such as
Home
,Work
,Favorite
, orRecent
. - hasPrivateCharger: A boolean value indicating whether the location has an EV charger available for private use.
The user profile ensures that Home
and Work
locations are unique locations. The UserLocations class automatically handles this constraint, ensuring at most one Home
and one Work
location are set at any time. If a new Home
or Work
location is added or an existing location is updated to have one of these types, any previous location having this same type will have it removed in order to maintain the constraint.
The uniqueness of Home
and Work
locations is maintained through the use of the UserLocations methods like create or update. These methods handle automatically the enforcement of this rule, ensuring that you don’t need to add additional logic to maintain their uniqueness.
Note: Whenever you change the user profile data, you must store the user profile in order to persist the changes, otherwise they will be lost when the application is closed.
Adding personal locations
You can add personal locations in the UserProfile in-memory object by using the create method of UserLocations.
For example, you can add a favorite location to the user profile as follows:
1userProfile.locations.create(2 types = setOf(PersonalLocationType.Favorite),3 place = Place(4 coordinate = GeoPoint(5 latitude = 52.379015,6 longitude = 4.9004167 ),8 address = Address(9 freeformAddress = "Central Station, Amsterdam, Netherlands"10 )11 ),12 name = "Train Station"13)
Note: Whenever you change the user profile data, you must store the user profile in order to persist the changes, otherwise they are saved only in memory and lost when the application is closed.
Next steps
Since you have learned the basics of personal data management, here are recommendations for the next steps: