Skip to content

Favorites

In this guide you will learn how to insert a landmark into the favorites, and also how to remove a landmark from the favorites.

Setup

First, get an API key token, see the Getting Started guide.

Download the Maps & Navigation SDK for Android archive file

Download the Favourites project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.

Favorites example Android screenshot

Favorites example Android screenshot

The heart icon shows that the landmark is added to the favorites when it is filled, and not added to the favorites when it is just a contour.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how the a landmark is added to, or removed from, the favorites.

In the class MainActivity : AppCompatActivity(), an instance of each of the following is created:
var store: LandmarkStore a landmark store, so the favorited landmark(s) can be written into the data folder;
val searchService = SearchService() a search service, so that the id of a result landmark can be obtained.
The override fun onCreate() starts by defining the name of a location to search for. Coordinates are not required.
val text = "Statue of Liberty New York"
val coordinates = Coordinates(0.0, 0.0) and then the search service is invoked:
searchService.searchByFilter(text, coordinates)
1if (results.isNotEmpty()) {
2    val landmark = results[0]
3    flyTo(landmark)
4    displayLocationInfo(landmark)
5}

The search service simply gets the first result landmark (index 0) and flies to it, also displaying its name and latitude and longitude coordinates.

 1imageView.setOnClickListener {
 2    val landmarkId = getFavouriteId(landmark)
 3    if (landmarkId != -1) {
 4        deleteFromFavourites(landmarkId)
 5        updateFavouritesIcon(imageView, false)
 6    } else {
 7        addToFavourites(landmark)
 8        updateFavouritesIcon(imageView, true)
 9    }
10}
A red heart icon is drawn as a contour if the landmark is not in the favorites. The red heart icon is filled if the landmark is in the favorites.
Clicking the red heart icon when it is drawn as a contour, causes it to be filled, and the landmark is added to the favorites.
Clicking the red heart icon when it is drawn filled, causes it to be drawn again as a contour, and the landmark is removed from the favorites.
Note that to remove a landmark from the favorites, its id is required.

To check if a landmark is already in the favorites, the fun getFavouriteId() function works by reverse geocoding. First, it gets the coordinates from the landmark, and then it searches within a radius of that landmark, and gets a list of landmarks in that radius from the favorites. Then it looks for the one closest to the coordinates of the input landmark, and returns the id of that landmark.

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git