Favourites
In this guide you will learn how to insert a landmark into the favourites, and also show to remove a landmark from the favourites.
The star 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.
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 resulted landmark can be obtained. The search service implements theonStarted
andonCompleted
callbacks. When the search completes, if the result list is not empty, then the first item in the result list (at index 0) is taken and the camera flies to the location of that result.
The flyTo(landmark)
shows on the map the searched landmark. After the search completes and the camera flies to the target, it is up to the user whether to add it to the favorites or not, using the button in the lower right corner of the viewport.
The displayLocationInfo()
function is called when the search completes with a non-empty result list. The function displays the name and coordinates of the landmark on screen, as well as a yellow star-shaped icon.
A click listener is set for the star-shaped icon, imageView.setOnClickListener
, so the landmark is added to the favorites if the icon is clicked and the landmark is not already in the favorites; and conversely, the landmark is removed from the favorites if the icon is clicked and the landmark is already in the favorites.
Note that to add or remove a landmark from the favorites, the ID of that landmark must be obtained, using the getFavouriteId()
function which does a linear search through all landmarks in a specified set, and looks for coordinates that match within a specified threshold.
MainActivity
overrides the onCreate()
function which checks that internet access is available, then calls the createStore()
function, which instantiates a Landmark store to for the favorites, and then does a search for a hardcoded target:
createStore()
val text = "Statue of Liberty New York"
val coordinates = Coordinates(0.0, 0.0)
searchService.searchByFilter(text, coordinates)
The coordinates of the target are not required for the search and can be set to zero.